我试图在Android应用程序中构建一个方法,该方法将导致活动每3分钟刷新一次,类似于您在twitter或Facebook类型的应用程序中看到的,其中应用程序将每隔几分钟刷新一次新闻源,但我无法在网上找到任何教程,让我知道如何做到这一点,任何帮助都会有很长的路要走!
答案 0 :(得分:5)
您需要创建一个在后台运行的异步更新任务,并触发您的更新方法,或者只需每3分钟通过一次意图直接重新启动活动。类似的东西:
private class YourUpdateTask extends AsyncTask<Integer, Void, Integer> {
/**
* The system calls this to perform work in a worker thread and delivers
* it the parameters given to AsyncTask.execute()
*/
protected Integer doInBackground(Integer... millis) {
try {
int waited = 0;
int duration = yourTimeHere;
while (waited < duration) {
Thread.sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
// do nothing
}
updateState();
return 1;
}
/**
* The system calls this to perform work in the UI thread and delivers
* the result from doInBackground()
*/
protected void onPostExecute(Integer result) {
refreshActivity();
}
}