简体   繁体   English

暂停Android应用,直到按下按钮

[英]Pause Android app until button press

I have two methods: go() and stop(), and a for loop looping through these methods 3 times. 我有两个方法:go()和stop(),以及一个for循环遍历这些方法3次。 go() activates automatically when the loop starts and stop() will only activate once a button is pressed 3 times: go()在循环开始时自动激活,并且stop()仅在按下按钮3次后才会激活:

private static int buttonPress;

for (int i = 0; i < 3, i++) {
    go();
    do {} while(pressCount < 4);
    stop();
}

Whenever the button the pressed, pressCount is incremented by 1: 每当按下按钮时,pressCount都会增加1:

public void button(View v) {
    pressCount++;
}

The problem is that with this setup, when the do while loop launches, the app freezes and crashes. 问题在于,使用此设置时,当do while循环启动时,应用程序冻结并崩溃。

Is there any way to fix this while still having the go() activate before stop(), having stop() activate after pressCount is greater than 3, and looping through 3 times? 有什么方法可以解决此问题,同时仍可以在stop()之前激活go(),在pressCount大于3之后激活stop()并循环3次?

Thanks 谢谢

如果发生这种情况,则阻塞主线程的时间不能超过5秒钟,然后会弹出anr(应用程序无响应)对话框。

try this 尝试这个

private boolean isStop = true;
private int buttonPressedCount = 0;

private void goOrStop() {
    if(isStop) {
      go();
      isStop = false;
    } else {
       stopIfCan(); // :)
    }
}

private void stopIfCan() {
    if(buttonPressedCount >= 3 ) {
        buttonPressedCount = 0; 
        isStop = false;
        stop();
    }
}

public void button(View v) {
    buttonPressedCount++;
}

You cannot pause main thread, the app freezes. 您无法暂停主线程,应用程序将冻结。

private int loopCount = 0;
private int pressCount = 0;

public void button(View v) { /* Runs when button is clicked */
    if (loopCount < 4){
        pressCount++;

        if (pressCount == 3){
            pressCount = 0;
            loopCount++;
            stop();
        }
    }
}

This code runs stop() when the button is pressed three times, but runs that only three times. 此代码在按下按钮3次时运行stop() ,但仅运行3次。 (After 9 presses nothing happens) (按9次后无任何反应)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何暂停程序直到按下按钮? - how to pause program until a button press? 如何禁用任何 android 应用程序,以便在我更改/按下应用程序中的某个按钮之前无法打开它? - How can i disable any android app such that it cannot be opened until i change/press some button in app? Android App按钮按下无法正常运作 - Android App button press not functioning correctly 当您未按一下播放时按暂停或停止时,Android广播流媒体应用程序崩溃 - Android radio streaming app crashes when you press pause or stop at the moment you haven't clicked play 如何在循环中暂停直到按下按钮 - How to pause while loop until a button is pressed 如何暂停for循环,直到按下按钮? - How to pause a for loop until a button is presses? 按下键盘上的App按钮 - App button as keyboard press 在android studio中按“后退”按钮后,应用程序仍然运行 - App remains running after press back button in android studio 谷歌助手是否可以在 Android 上的某个应用程序中按下按钮? - Is it possible for google assistant to press a button in a certain app on Android? 当我按下录制按钮时,android 应用程序在 6 秒后崩溃 - the android app crashes after 6 seconds when I press record button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM