AsyncTask 不会停止

发布于 2024-11-29 13:27:59 字数 834 浏览 0 评论 0原文

我创建了一个 AsyncTask,它从数据库加载消息历史记录,然后将其显示在设备屏幕上:

private void loadHistoryFromDB(Date lastUpdateDate)
    {
    final class DBAsyncTask extends AsyncTask<Void, Void, List<XMPPMessage>>
    {
        @Override
        protected List<XMPPMessage> doInBackground(Void... arg0) 
        {
            List<XMPPMessage> messages = null;
            try 
            {
                messages = PersistenceManager.getXMPPMessagesFromDB(userInfo, 0, messagingActivity);
            }
            catch (SQLException e) 
            {
                e.printStackTrace();
            } 
            catch (LetsDatabaseException e)
            {
                e.printStackTrace();
            }
            return messages;
        }

它似乎工作正常,但执行后,它留下 2 个正在运行的线程,因此我无法完成活动。我该如何修复它?

I've created an AsyncTask that loads messaging history from a database and then shows it on the device screen:

private void loadHistoryFromDB(Date lastUpdateDate)
    {
    final class DBAsyncTask extends AsyncTask<Void, Void, List<XMPPMessage>>
    {
        @Override
        protected List<XMPPMessage> doInBackground(Void... arg0) 
        {
            List<XMPPMessage> messages = null;
            try 
            {
                messages = PersistenceManager.getXMPPMessagesFromDB(userInfo, 0, messagingActivity);
            }
            catch (SQLException e) 
            {
                e.printStackTrace();
            } 
            catch (LetsDatabaseException e)
            {
                e.printStackTrace();
            }
            return messages;
        }

It seems to work fine, but after being executed, it leaves 2 running threads and I can't finish the activity because of that. How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

捂风挽笑 2024-12-06 13:27:59

只要您的任务正确执行(从 onPostExecute 退出),您就不必担心这一点。执行后,AsyncTask 线程将以线程池或单线程的形式保留下来,以便可能重用,具体取决于平台版本。这是正常行为 - 它们最终会被清理/重复使用。

As long as your tasks are executing properly (exits from onPostExecute), this shouldn't be something you have to worry about. Once executed, AsyncTask thread(s) will stick around for possible reuse in the form of a thread pool or single thread, depending on platform version. This is normal behaviour - they will eventually be cleaned-up/reused.

嗫嚅 2024-12-06 13:27:59

首先,确保您在重写方法调用的顶部调用 super.doInBackGround()

如果不是这样,可能是因为您正在维护与数据库的连接。

也就是说,您仍然在数据库上建立了锁。

看看是否可以显式解锁数据库,这可能会解决您的问题。

您可以将其放入 onPostExecute() 方法中。

First off, make sure you are calling super.doInBackGround() at the top of your overridden method call.

If that isn't it, it's likely because you are maintaining the connecting to the database.

That is, you still have a lock established on the database.

See if you can explicitly unlock the database, that may fix your problem.

You could put it in the onPostExecute() method.

似梦非梦 2024-12-06 13:27:59

此问题很可能是由于 AsyncTask 的 cancel 方法的混乱造成的。

您需要将后台任务分解为可循环的段,然后在每次循环迭代开始执行任务之前,您需要检查任务是否被取消,如果是则需要中断循环。似乎没有任何其他方法可以阻止 AsyncTask 的执行。

我在这里发布了有关此问题的详细指南和代码示例:

http://tpbapp.com/android-development/android-asynctask-stop-running-cancel-method/

This problem is most likely due to confusion surrounding the cancel method of AsyncTask.

You need to break down your background task into loopable segments, then Before each loop iteration starts doing your task,you need to check if the task is cancelled and if it is you need to break the loop. There doesn't seem to be any other way to stop an AsyncTask from executing.

I've posted a detailed guide to this problem with code examples here:

http://tpbapp.com/android-development/android-asynctask-stop-running-cancel-method/

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