每隔5秒自动请求数据,或刷新。。。
@Override
protected void onResume() {
super.onResume();
// 每隔五秒刷新data
timer = new Timer();
timer.schedule(new TaskJob(), 0, 5000);
}
//没5秒后需要做的事
private class TaskJob extends TimerTask {
Handler handler = new Handler();
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
//TODO 具体要做的事
}
});
}
}
@Override
protected void onPause() {
super.onPause();
if (timer != null) {
timer.cancel();
}
}
如果不
timer
.cancel()
;会出现
异常
:
android.view.WindowManager$BadTokenException: Unable to add window — token android.os.BinderProxy@438e7108 is not valid; is your activity running?