我试图在 Android 应用程序中构建一个方法,该方法将导致 Activity 每 3 分钟刷新一次,类似于您在 Twitter 或 Facebook 类型的应用程序中看到的,应用程序将每隔几分钟自行刷新新闻源,但是我无法在网上找到任何教程让我了解如何完成此操作,任何帮助都会大有帮助,谢谢!
最佳答案
您需要创建一个异步更新任务,该任务在后台运行并触发您的更新方法,或者每 3 分钟通过 Intent 直接重新启动 Activity 。像这样的东西:
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();
}
}
关于java - 在android中构建自动刷新方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7411031/