如何实现执行 AsyncTask 的 Timer/TimerTask? (安卓)

发布于 2024-11-09 16:03:28 字数 1195 浏览 4 评论 0原文

我正在尝试在指定时间(即每隔几秒,尽管此速率可能在运行时发生变化)异步且重复地执行任务(即从文本文件加载数据)。

我做了一些研究,并决定这需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟凡古楼 2024-11-16 16:03:28

你让这条路变得比需要的更加困难。 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 an AsyncTask just put the code you want to run in the TimerTask.run() method.

秋心╮凉 2024-11-16 16:03:28

如果您想通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文