private Timer timer;
private TimerTask task;
先定义这两个
//新建一个计时任务
task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
message.what = 1;
handler1.sendMessage(message);
}
};
//计时器启动
timer.schedule(task, 4000);
//handler1处理计时器要做的事
handler1 = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what == 1) {
handler.removeCallbacks(update);
start.setEnabled(true);
}
super.handleMessage(msg);
}
};
timer = new Timer();
if (timer != null) {
if (task != null) {
task.cancel();//移除原有的计时任务
}
}
//定时器启动前要移除上一个定时器