private void startUpdateTimerTask() {
TimerTask task = new TimerTask() {
@Override
public void run() {
doUpdate();
}
};
Timer timer = new Timer(true);
timer.schedule(task, ONE_MINUTE_MILLIS, ONE_HOUR_MILLIS);
}
private void doUpdate() {
new AsyncTask<Void,Void,Void>() {
@Override
protected Void doInBackground(Void... params) {
//....Network time-consuming tasks
return null;
}
}.equals();
}
(1)我的问题:当我运行这个函数时,会有RuntimeException(No Looper; Looper.prepare() was not called on this thread.);
所以我改变了:
private void startUpdateTimerTask() {
TimerTask task = new TimerTask() {
@Override
public void run() {
Looper.prepare();
doUpdate();
Looper.loop()
}
};
Timer timer = new Timer(true);
timer.schedule(task, ONE_MINUTE_MILLIS, ONE_HOUR_MILLIS);
}
那么 RuntimeException 没有出现,但是 doUpdate() 只执行了一次?
(2)问题:如何实现上网1小时更新一次信息?