How to call asyncTasks periodically(如何定期调用 asyncTasks)
问题描述
我有两个 AsyncTask 进行网络操作.我想定期给他们打电话(比如一分钟后).我怎么做?我不认为我可以在 UI 线程上做到这一点.我需要创建一个新线程吗?是否可以在没有 AlarmManager/Service 的情况下实现这一点?
I have two AsyncTasks doing network operations. I want to call them periodically (like after one min.). How do I do that? I dont think I can do it on the UI thread. Do i need to create a new thread? Is it possible to implemet this without AlarmManager/Service?
基本上我想在一分钟后定期执行这两个语句.
Basically I want to exectue these two statements periodically after one min.
new UploadAsyncTask().execute();
new DownloadAsyncTask().execute();
谢谢
推荐答案
只需使用计时器.
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
new UploadAsyncTask().execute();
new DownloadAsyncTask().execute();
}
});
}
};
timer.schedule(task, 0, 1000); //it executes this every 1000ms
这篇关于如何定期调用 asyncTasks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何定期调用 asyncTasks


基础教程推荐
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01