1.新建handler及runnable对象:
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what == MSG_UPDATE) {
//todo
}
super.handleMessage(msg);
}
};
private Runnable task = new Runnable() {
@Override
public void run() {
mHandler.sendEmptyMessage(MSG_UPDATE);
mHandler.postDelayed(this, DELAY);
}
};
2.启动任务及停止任务:
@Override
protected void onResume() {
super.onResume();
mHandler.postDelayed(task, DELAY);
}
@Override
protected void onPause() {
super.onPause();
mHandler.removeCallbacks(task);
}