AsyncTask 不会停止
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要您的任务正确执行(从
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.首先,确保您在重写方法调用的顶部调用
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.此问题很可能是由于 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/