Asking an IntentService for information about its queue

懵懂的女人 提交于 2019-12-17 15:58:16

问题


I have an IntentService that queues up web service calls that need to be made to my web server. So each Intent is a web service call to be made.

I'd like to set something up where my application can ask this IntentService if it has any Intents containing a particular piece of data (IE: "Are you already waiting to ask the cloud for x data? Or do I need to tell you to do it?").

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?

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.


回答1:


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.




回答2:


Another solution could be to solve it by overriding onStartCommand.

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



来源:https://stackoverflow.com/questions/5357013/asking-an-intentservice-for-information-about-its-queue

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