Android中定时任务的实现
- 采用Handler的postDelayed(Runnable, long)方法
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this,2000);
}
};
启动定时器:
handler.postDelayed(runnable,2000);
关闭定时器:
handler.removeCallbacks(runnable);