我正在正确执行活动的定时任务,如下所示。但是,当活动 onResume 时,会抛出一个异常,“定时器任务已安排”。一旦执行任务,我就会取消计时器。如何解决?谢谢你
final Runnable setButton = new Runnable() {
public void run() {
myClass.aBridge.button_back.setVisibility(View.INVISIBLE);
timer.cancel();
}
};
TimerTask task = new TimerTask(){
public void run() {
webPush.this.runOnUiThread(setButton);
}
};
@Override
protected void onResume() {
super.onResume();
timer = new Timer();
timer.schedule(task, 5000);
}
任务在 onResume 之前被调用一次:
timer = new Timer();
timer.schedule(task, 5000);