AsyncTask的不停止 [英] AsyncTask doesn't stop

查看:115
本文介绍了AsyncTask的不停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个从数据库载入历史的消息,然后显示在设备屏幕上的AsyncTask:

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;
        }

这似乎做工精细,但在运行后,它留下2运行的线程,我无法完成,因为该活动。我该如何解决?

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?

推荐答案

只要你的任务是正确执行,这不应该是什么(从 onPostExecute 退出)你不用担心。一旦执行,的AsyncTask 线程(S)将坚持围绕将来可能重新使用一个线程池或单个线程的形式,这取决于平台版本。这就是正常的行为 - 他们最终将被清理后/再利用

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.

这篇关于AsyncTask的不停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆