Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

android - Asking an IntentService for information about its queue

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...