Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 789
Andrew Avatar asked Mar 18 '11 19:03

Andrew


People also ask

How do I get context in IntentService?

How do I get the context in an IntentService? Here is a snippet of the receiver and the scheduling service. public class BackupAlarmReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, BackupSchedulingService.

What is the purpose of the IntentService class?

IntentService is an extension of the Service component class that handles asynchronous requests (expressed as Intent s) on demand. Clients send requests through Context.

Can an IntentService execute multiple tasks sequentially?

The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.


1 Answers

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.

like image 190
CommonsWare Avatar answered Nov 03 '22 00:11

CommonsWare