Asking an IntentService for information about its queue

♀尐吖头ヾ 提交于 2019-11-27 20:49:57

Are there any suggestions on how I might extend IntentService to do this? Can the Intent queue of an IntentService be traversed? Or would I need to take the IntentService code and alter it?

Probably the latter, if you really want to examine the actual queue. IntentService turns the Intents into messages popped on the message queue of a Looper via a HandlerThread. None of that is exposed through the SDK, though. Fortunately, IntentService is fairly short -- not even 150 lines. You could clone it into your own package and make modifications as needed. Just run a diff on the source when new source updates are released in the AOSP repository, so you know if Google performed any major surgery on IntentService itself that you might want to take advantage of.

The only other idea I have is to add a table to the database and log which calls are in the queue, with each log being removed from the table when completed.

It wouldn't even need to be persistent like that, since the IntentService's own queue is not persistent. Just maintain your own FIFO queue in onStartCommand(), chaining to the superclass, and popping your Intent off the queue at the end of onHandleIntent(). This has a chance of getting out of sync with the master queue (e.g., you'll want to use finally to ensure you pop the work off your own queue), but you wouldn't have to clone the class.

johan

Another solution could be to solve it by overriding onStartCommand.

Here is a smooth solution in which I found inspiration for a similar problem :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!