1. 定义定时器、定时器任务及Handler句柄
private final Timer timer = new Timer();
private TimerTask task;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
// 要做的事情
super.handleMessage(msg);
}
};
2. 初始化计时器任务
task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
3. 启动定时器
timer.schedule(task, 2000, 2000);
4. 停止计时器
timer.cancel();
android 采用Handler与timer及TimerTask结合的方法完成定时任务
最新推荐文章于 2024-07-23 07:32:35 发布