取消暂停/等待后延迟的处理程序/可运行,仍可以激活暂停的代码

| 设置:我有一个4阶段的活动,我使用一个后延迟处理程序来暂停两个嵌套的后延迟。 时间线: stage1 | CODE FIRES | stage2 |代码火灾| stage3 |代码火灾|阶段4 阶段1:已停用,仅显示自定义警报对话框 阶段2:停用(主要是)为用户提供视觉效果,包括倒数计时器 阶段3:使用第二个倒数计时器停用 阶段4:用户互动,无倒计时或其他暂停 我希望用户能够通过按一下按钮跳到stage2的末尾,这将同时取消倒计时计时器和后延迟(而不会取消等待启动的代码) 我曾尝试分别取消两者,但都没有成功,这是我使用过的命令。
    countdowntimer.cancel();  // this didn\'t work from the button or a void call
    handler.removeCallbacks(runnable); // want to skip to end of stage2
我看到了一些有关try / catch的引用,或者在搜索中使用了线程,但不确定如何使用或是否合适。 感谢您的任何投入。 **编辑非工作代码参考:* 我裁掉了大部分无关的内容,不确定是否杀死了}或;额外
    public class Main extends Activity {

public Boolean onOffCDT = true;
public Handler handler2Mem;
public Runnable mem2Runnable = null;
public CountDownTimer aCounter;
@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    aCounter = null;        
    final Handler handlerMem = new Handler(); 
    final Runnable memRunnable = new Runnable() {

        public void run() { 
        //code
              final CountDownTimer aCounter = new CountDownTimer(memTime, 100) {

                 public void onTick(long millisUntilFinished) {
                     //update time
                     if(onOffCDT.equals(false)) {
                         onOffCDT = true;
    //                   aCounter.cancel();
                     }   
                 }

                 public void onFinish() {
                     title.setText(tempTitle);
                 }
        };
             aCounter.start();            


    Handler handler2Mem = new Handler(); 
    Runnable mem2Runnable = new Runnable() {
        public void run() {
             //code
             CountDownTimer bCounter = new CountDownTimer(8000, 100) {

                 public void onTick(long millisUntilFinished) {
             //update time
                 }

                 public void onFinish() {
                     title.setText(tempTitle);
                 }
              };
             bCounter.start(); 
             stage3(8000);   //another delay            
        };
    };  handler2Mem.postDelayed(mem2Runnable, memTime);


    }    

}; handlerMem.postDelayed(memRunnable, 2500);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //  this button checks stage#, if stage4 info - else...       
          }else{
          aCounter.cancel();
              onOffCDT = false;
              cancelCountDown(mem2Runnable, handler2Mem);
          }
        }

    });

    }

private void stage3 (Integer tTime) {
    Handler handler = new Handler(); 
            handler.postDelayed(new Runnable() { 
            public void run() { 
              //Do Stuff 

         } 
    }, tTime); 
}

public void cancelCountDown(Runnable run2, Handler hand2) {

      hand2.removeCallbacks(run2);

}
    
已邀请:
        只需在方法中添加
if ((boolean)canceled)return
并在要取消时将其设置为true     

要回复问题请先登录注册