如何停止android中的ASyncTask线程
can anybody have any idea how to stop ASyncTask thread in android?.
实际上我有一个循环来创建线程并执行它们。当这个循环结束时,我想停止所有已运行的线程。 有什么办法可以停止线程吗?
多谢。
can anybody have any idea how to stop ASyncTask thread in android?.
Actually i have a loop which creates threads and executes them. and when this loop will end i want to stop all that threads which have run.
is there anyway to stop threads?
thanks alot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我使用
AsyncTask
的经验,调用cancel(boolean mayInterruptIfRunning)
并不一定会停止后台进程的执行。似乎发生的事情是AsyncTask
将执行onCancelled()
,并且不会运行onPostExecute()
> 当它完成时。该行为可能完全取决于doInBackgound()
中的代码In my experience with
AsyncTask
, callingcancel(boolean mayInterruptIfRunning)
doesn't necessarily stop the execution of the background process. All that seems to happen is that theAsyncTask
will executeonCancelled()
, and won't runonPostExecute()
when it completes. The behaviour probably depends on exactly what code you have indoInBackgound()
就我而言,我使用 HttpPost、HttpGet 等执行服务器请求。我发现结束 AsyncTask 的唯一方法是调用 abort() 方法,如以下示例所示
In my case I perform server requests with HttpPost, HttpGet and so on. The only way I found to end an AsyncTask was calling the abort() method as you can see in the following example