我需要的是当Competition.isTimerStarted变为false时,该可运行项应启动一次buttonProcedure()并自行停止
但这不会发生-永远调用buttonProcedure()并且runnable不会停止。

 timerRun=new Runnable()
            {
                 @Override
                 public void run() {

                     timerTVmainmenue.setText(competition.getTimerTime());
                     if (!competition.isTimerStarted) {thandler.removeCallbacks(timerRun);Log.d("MyLog","timerrun is blame for infinite butproc");buttonsProcedure();}
                thandler.postDelayed(timerRun, 243); //looping this for every 243 ms
                 }
            };


我该如何正确停止?

最佳答案

        timerRun=new Runnable()
        {
             @Override
             public void run() {

                 timerTVmainmenue.setText(competition.getTimerTime());
                 if (!competition.isTimerStarted){
                thandler.removeCallbacks(this);
                Log.d("MyLog","timerrun is blame for infinite butproc");
                 buttonsProcedure();
                 return;
             }
            thandler.postDelayed(this, 243); //looping this for every 243 ms
             }
        };


您必须在if条件内添加return或使用if-else构造

07-24 15:17