如何在固定时间间隔后重复执行异步任务 [英] How to execute Async task repeatedly after fixed time intervals
本文介绍了如何在固定时间间隔后重复执行异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何让异步任务在一段时间后重复执行,就像定时器一样......实际上我正在开发一个应用程序,它将自动从服务器下载所有最新的未读问候语,为此我必须检查来自服务器的更新经过一些固定的时间间隔......我知道这可以通过计时器轻松完成,但我想使用异步任务,我认为这对 android 应用程序更有效.
How to make Async task execute repeatedly after some time interval just like Timer...Actually I am developing an application that will download automatically all the latest unread greeting from the server and for that purpose I have to check for updates from server after some fixed time intervals....I know that can be easily done through timer but I want to use async task which I think is more efficient for android applications.
推荐答案
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
// PerformBackgroundTask this class is the class that extends AsynchTask
performBackgroundTask.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
}
这篇关于如何在固定时间间隔后重复执行异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文