【问题标题】:I can not stop a Runnable using Handler in JAVA我无法在 JAVA 中使用 Handler 停止 Runnable
【发布时间】:2022-01-16 21:02:01
【问题描述】:

我正在尝试构建一个秒表,并且我有一个名为 start 的按钮开始计数,在它开始后按钮的名称更改为停止,当我按下停止时我想暂停计数,但是当我尝试时运行newHandler.removeCallbacks(updateTimerThread);程序继续计数,在重置按钮上此方法有效。

public class MainActivity extends AppCompatActivity {

    Button start_button, reset_button;
    TextView time_view;
    long start_time = 0, time_in_miliseconds = 0, update_time = 0, prev_time = 0;
    final Handler newHandler = new Handler();

    Runnable updateTimerThread = new Runnable() {
        private boolean isActive;
        @Override
        public void run() {

            if(isActive)
            {
                prev_time += time_in_miliseconds;
                newHandler.removeCallbacks(updateTimerThread);
                start_button.setText("Start");
                //time_view.setText(""+save_hour+":"+String.format("%2d",save_min)+":"+String.format("%2d",save_sec)+"."+String.format("%3d",save_centisec));
            }
            else
            {
                time_in_miliseconds = SystemClock.uptimeMillis() - start_time;
                update_time = time_in_miliseconds + prev_time;
                int sec = (int) (update_time / 1000);
                int min = sec / 60;
                int hour = (int) sec / 3600;
                sec %= 60;
                int centisec = (int) (update_time / 10) % 100;
                time_view.setText(""+hour+":"+String.format("%2d",min)+":"+String.format("%2d",sec)+"."+String.format("%3d",centisec));
                newHandler.postDelayed(this, 10);
            }
            isActive = !isActive;
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start_button = (Button) findViewById(R.id.start_button);
        reset_button = (Button) findViewById(R.id.reset_button);
        time_view = (TextView) findViewById(R.id.time_view);

        start_button.setOnClickListener(view -> {
            start_time = SystemClock.uptimeMillis();
            start_button.setText("Stop");
            newHandler.postDelayed(updateTimerThread,0);
        });

        reset_button.setOnClickListener(view -> {
            // Here the Runnable is stopped
            newHandler.removeCallbacks(updateTimerThread);
            start_time = 0;
            prev_time = 0;
            time_view.setText("0:0:0.0");
            start_button.setText("Start");
        });

【问题讨论】:

    标签: java android callback android-handler


    【解决方案1】:

    请删除您正在检查start_button.isPressed() 的所有条件块,它不起作用。您可以使用附加标志isRunning 来指示时钟是否正在运行:

    private boolean isRunning;
    
    start_button.setOnClickListener(view -> {
            if (isRunning) {
                newHandler.removeCallbacks(updateTimerThread);
                start_button.setText("Start");
            } else {
                start_time = SystemClock.uptimeMillis();
                start_button.setText("Stop");
                newHandler.post(updateTimerThread);
            }
            isRunning = !isRunning;
        });
    

    【讨论】:

    • 我用您的解决方案更新了代码,但它仍然无法正常工作,它只是在启动后立即停止时钟。您可以在原始问题中看到我更新的代码
    • 请注意我的代码在start_button.setOnClickListener 块中,而不是在updateTimerThread runnable 中,就像您更新的问题一样。
    【解决方案2】:

    你的计时器需要一些状态。你可以这样做(添加'boolean isActive'):

            Runnable updateTimerThread = new Runnable() {
                boolean isActive = true;
                @Override
                public void run() {
                    if (isActive) {
                        time_in_miliseconds = SystemClock.uptimeMillis() - start_time;
                        update_time = time_in_miliseconds + prev_time;
                        // ...
                    }
                    if (start_button.isPressed()) {
                        isActive = !isActive;
                        // Do some other stuff
                    }
                }
            };
    

    或者也许使用状态模式。

    【讨论】:

      猜你喜欢
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      相关资源
      最近更新 更多