如何取消Service/IntentService/AsyncTask/Looper
我在这里要疯了。 我想要一件简单的事情 - 我有一个很长的任务(从网络上获取多个数据对象)并且我希望能够取消它。 我尝试了很多东西(很多)但没有任何效果
流程是这样的:
用户单击按钮
我开始工作(我尝试使用< code>AsyncTask、
Service
、IntentService
和Looper
)该任务负责处理所有事务,包括添加进度更新的持续通知
通知中的意图调用了一个新活动,她的唯一目的是取消正在进行的任务
- 我尝试调用的cancelActivity中
stopService()
对于 Service/IntentService 或执行Looper.quit()
for Looper (我不记得我为 AsyncTask 尝试了什么,不确定是否有这样的 api 来取消它)
在我看来,最好的选择是使用 < code>IntentService (我可以排列多个任务,IntetService 会按照我想要的顺序执行)但是 我愿意接受任何类型的实现建议 - 我不在乎代码是什么,只是我可以选择取消任务
提前感谢您的帮助
Dror
(我要去睡觉了 -在同一个问题上花费 8 个小时实在是太多了)
I'm going nuts here.
I want a simple thing - I have a long task (fetching several data objects from the web) and I want the ability to cancel it.
I tried a lot of things (a lot) and nothing works
The flow goes like this:
the user click on a button
I start the work (I tried with
AsyncTask
,Service
,IntentService
andLooper
)the task takes care of everything including adding ongoing notification for progress updates
the intent in the notification has a call for a new activity that her only purpose is to cancel the ongoing task
in the cancelActivity I tried to call
stopService()
for Service/IntentService or doLooper.quit()
for the Looper (I don't remember what I tried for AsyncTask, not sure if there is such api for canceling it)
In my point of view the best option will be using IntentService
(I could have several task lining up and IntetService will do it in order like I want) but
I'm open to suggestions for any type of implementation - I don't care what the code will be, just that I will have the option to cancel the task
Thank you in advance for your help
Dror
(I'm off to bed - 8 hours on the same issue is just too much)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您无法识别后台逻辑中的停止条件,那么使用什么特定操作来停止任务并不重要。彻底实现这一目标的唯一方法是后台工作人员善意地停止并退出。
您可以使用几种可能的场景和解决方案来取消后台工作。
interrupt()
,在异常处理程序中捕获异常,并采取适当的步骤干净地完成此任务并退出。无论如何,如果任务不知道被停止的可能性,就没有办法停止你的任务(除了杀死非常糟糕的线程)。
It does not matter what specific operation you use to stop the task if you don't recognize the stop condition in your background logic. The only way to cleanly accomplish it is if background worker stops and exits in good faith.
There are few possible scenarios and solutions that you can use for canceling background work.
isInterrupted()
) between operations and exit if this flag indicates that operation must stop.interrupt()
on background thread, catch exception in in exception handler make appropriate steps to finish this task cleanly and exit.In any case, there is no way (except killing the thread which is really bad) to stop your task if it does not know about possibility of being stopped.
好吧,我设法做了一些接近我想要的事情。
我正在使用 IntentService 来对我的任务进行排队。每个新任务都是
AsyncTask
。 AsyncTask 首先发送带有用于 cancelActivity 的未决意图的通知。单击通知时,用户会收到有关停止任务的警告弹出窗口。如果他点击“是”,我就会在 IntentService 上执行
stopService()
操作。在 IntentService 中我添加了:
在 AsyncTask 中我添加了:
这样就会删除当前正在运行的所有连接。在执行工作的实际代码中,我需要捕获连接关闭的异常并处理它,
所以最后我实际上并没有停止 AsyncTask/Service ,而是公开
httpClient
所以我将为什么能够异步断开连接。我觉得有点难看,但我没有其他办法,
谢谢大家
Ok, i manged to do something close to what I want.
I'm using IntentService that will queue my task. each new task is
AsyncTask
. the AsyncTask starts with sending notification with pendingIntent for cancelActivity.When clicking on the notification the user gets a warning popup about stopping the task. If he clicks yes than I do
stopService()
on my IntentService.In the IntentService I added:
in the AsyncTask I added:
so that will drop all the connection currently in motion. In the actual code that do the work I need to catch the exception for connection shutdown and handle it
so in the end I'm not actually stopping the AsyncTask/Service but rather exposing the
httpClient
so I will be able to drop the connection in asynchrony why.I think is a bit ugly but I got no other way
thank you all