在 IntentService 处理时删除意图
如果我正确理解 IntentService 上的 API 文档,发送到 IntentService 的意图如果正在处理并且按顺序处理,则会排队。
有没有一种简单的方法可以告诉 IntentService 它应该在处理时删除发送给它的任何意图?
干杯, 托斯顿
If I understand the API docs on IntentService correctly, intents sent to an IntentService queue up if it is processing and are processed sequentially.
Is there an easy way to tell IntentService that it should just drop any intents sent to it while it is processing?
Cheers,
Torsten
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,据我所知,这不是一个简单的方法,但也许也不是太难。
我想我见过一个与此类似的问题,一个建议是通过从 Android 源代码复制代码来简单地定义您自己的 IntentService 类(代码少于 150 行,其中一些是注释)。
Intents 的“队列”由一个名为 ServiceHandler 的嵌套类管理,该类扩展了 Handler。如果您从源代码定义自己的“IntentService”,您可以添加方法来清除消息队列,甚至在处理程序正在处理 Intent 的过程中终止处理程序(如果这是您需要的)。
No, not an easy way that I know of but perhaps not too difficult.
I think I've seen a similar question to this and one suggestion was to simply define your own IntentService class by copying the code from the Android source (there's less than 150 lines of code and some of that is comments).
The 'queue' of Intents is managed by a nested class called ServiceHandler which extends Handler. If you define your own 'IntentService' from the source you could add methods to clear the message queue and even terminate the Handler if it is in the middle of processing an Intent (if that's what you need).
迟到总比不到好。是的,有一个简单的方法。覆盖
onStart()
,如果您不想处理 Intent,则返回,否则调用super.onStart()
。但请注意,
IntentService
的 javadocs 特别指出 onStart() 不应被重写。better late than never. yes, there's an easy way. override
onStart()
, and return if you don't want to process the intent, or callsuper.onStart()
otherwise.note however that the javadocs for
IntentService
specifically say that onStart() should not be overriden.