如何后固定的时间间隔重复执行异步任务 [英] How to execute Async task repeatedly after fixed time intervals
本文介绍了如何后固定的时间间隔重复执行异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使异步任务一定的时间间隔,就像计时器后,多次执行...其实我开发,将所有最新的未读的问候服务器,并为此目的我要检查来自服务器的更新自动下载应用程序一些固定的时间间隔后....我知道,可通过定时器很容易做,但我想用异步任务,我认为这是Android应用程序更有效率。
解决方案
公共无效callAsynchronousTask(){
最终的处理程序处理程序=新的处理程序();
定时器定时=新的Timer();
TimerTask的doAsynchronousTask =新的TimerTask(){
@覆盖
公共无效的run(){
handler.post(新的Runnable(){
公共无效的run(){
尝试 {
PerformBackgroundTask performBackgroundTask =新PerformBackgroundTask();
// PerformBackgroundTask这个类是扩展AsynchTask类
performBackgroundTask.execute();
}赶上(例外五){
// TODO自动生成的catch块
}
}
});
}
};
timer.schedule(doAsynchronousTask,0,50000);在每50000毫秒//执行
}
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屋!
查看全文