如何实现执行 AsyncTask 的 Timer/TimerTask? (安卓)
我正在尝试在指定时间(即每隔几秒,尽管此速率可能在运行时发生变化)异步且重复地执行任务(即从文本文件加载数据)。
我做了一些研究,并决定这需要 AsyncTask 或单独的线程。为了简单起见,我决定使用 AsyncTask。
我现在需要根据重复计时器计划执行此 AsyncTask。我相信我必须使用 Timer 和 TimerTask。
下面的代码是我想要实现的目标的简单形式。当我尝试使用 Android 模拟器(通过 Eclipse IDE)运行此代码时,我收到以下消息:“抱歉!应用程序已意外停止。请重试。”
我想知道问题出在哪里以及如何解决它。谢谢!
public class Sample extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SimpleTimerTask myTimerTask = new SimpleTimerTask();
long delay = 0;
long period = 5000;
Timer myTimer = new Timer();
myTimer.schedule(myTimerTask, delay, period);
}
private class SimpleAsyncTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}
}
private class SimpleTimerTask extends TimerTask {
public void run() {
new SimpleAsyncTask().execute();
}
}
}
编辑:以下是似乎相关的 LogCat 消息
致命异常:Timer-0
java.lang.ExceptionInInitializerError
at ...
原因:java.lang.RuntimeException:无法在未调用 Looper.prepare 的线程内创建处理程序()
在 ...
I'm trying to perform a task (ie. load data from a text file) asynchronously and repeatedly at specified times (ie. every few seconds, though this rate may change at runtime).
I have done some research and decided that this will require either an AsyncTask or a separate Thread. I have decided to use an AsyncTask for simplicity.
I now need to execute this AsyncTask according to a repeating timer schedule. I believe I must make use of a Timer and a TimerTask.
The code below is a simple form of what I am trying to achieve. When I try to run this code using an Android emulator (through the Eclipse IDE) I get the following message: "Sorry! The application has stopped unexpectedly. Please try again."
I would like to know where the problem arises and how I can fix it. Thanks!
public class Sample extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SimpleTimerTask myTimerTask = new SimpleTimerTask();
long delay = 0;
long period = 5000;
Timer myTimer = new Timer();
myTimer.schedule(myTimerTask, delay, period);
}
private class SimpleAsyncTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}
}
private class SimpleTimerTask extends TimerTask {
public void run() {
new SimpleAsyncTask().execute();
}
}
}
EDIT: Here are the LogCat Messages that seem to be relevant
FATAL EXCEPTION: Timer-0
java.lang.ExceptionInInitializerError
at ...
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你让这条路变得比需要的更加困难。 TimerTask 已经在它自己的线程上运行,因此您不需要使用 AsyncTask 只需将要运行的代码放入 TimerTask.run( ) 方法。
You're making this way harder than it needs to be. The
TimerTask
already runs on it's own thread, so you don't need to use anAsyncTask
just put the code you want to run in theTimerTask.run()
method.如果您想通过 OnPostExecute 方法更新 UI,则将 timertask 与 asynctask 结合使用有一个严重的限制。计时器在单独的线程中运行,因此您必须找到一种方法从主线程启动异步任务。
There is a serious limitation of using timertask with asynctask if you want to update UI from OnPostExecute method. A timer is run in separate thread, so you have to find a way to start an asynctask from main thread.