这个想法是我的runnable每分钟运行一次。每20秒运行一次,而不是指定的延迟运行
相反,它运行大约20秒钟,我不知道为什么。
下面是代码:
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
try{
//Post from Queue & update post
if (NetworkUtils.isConnected()) {
//post from queue
try {
postHelper.postFromQueue();
} catch (IOException e) {
e.printStackTrace();
}
//Update posts
postHelper.updateSolicitations();
}
}
catch (Exception e) {
// TODO: handle exception
}
finally{
//also call the same runnable to call it at regular interval
handler.postDelayed(this, 60000);
}
}
};
我不知道,如果是相关的,但它MainActivity的onCreate方法。
你的代码是确定的。 https://stackoverflow.com/questions/6242268/repeat-a-task-with-a-time-delay – dit
从onPause/onStop删除处理程序的回调文件 – Selvin
@dit这是一个更好的方式来做到这一点,它工作顺利, 谢谢! – Rosenberg