停止 IntentService
有谁知道是否有一种方法可以停止 IntentService 而无需完成其工作线程并自行停止?
简单的问题,但我在文档中找不到答案。有没有简单的方法可以阻止它?
谢谢
Does anyone know if there is a way of stopping an IntentService without it finishing its work thread and stopping itself?
Simple question, but I couldn't find the answer in the documentation. Is there a simple way of stopping it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在调用 StartCommand 时将发送给服务的消息放入队列之前。它转发消息以进行排队。所以你可以覆盖 onStartCommand,类似这样:
干杯
bevor a message to a service is enqueued onStartCommand is called. which forwards the message for queueing. so you could just override onStartCommand, something like that:
cheers
您应该能够调用 stopSelf();
http://developer.android.com/reference/android/app/Service .html#stopSelf()
You should be able to call stopSelf();
http://developer.android.com/reference/android/app/Service.html#stopSelf()
我目前偶然发现了我正在开发的应用程序的这一要求。我将尝试使用 onStartCommand 向意图服务发送消息以停止工作(例如,设置布尔标志 stopWork = true),并在工作作业期间或在下一个排队任务之前对其进行评估。 IntentService 不会立即停止,但会跳过所有挂起的任务。希望有帮助。我自己也想尝试一下。
I currently stumble upon this requierement for an app i am working on. I will try using onStartCommand to send a message to the Intent Service to stop working (for example, setup a boolean flag stopWork = true) and evaluate it during the working job or before the next queued task. The IntentService wont stop inmediately but will skip all pending tasks. Hope it helps. Gonna try it myself also.