设置:我有一个4阶段活动,我使用一个postdelayed处理程序暂停两个嵌套的后延迟。
时间轴:
stage1 | CODE FIRES | stage2 | CODE FIRES | stage3 | CODE FIRES | STAGE4
stage1:停用,只显示自定义alertdialog
阶段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);
}
答案 0 :(得分:0)
只需在方法中添加if ((boolean)canceled)return
,并在取消