6

In my app I use an IntentService to do some work. I want to find out how many intents are waiting to be processed, as IntentService holds them in a 'work queue' , and sends the next one to onStartCommand() as the onStartCommand of the previous one has finished.

How can I find out how many Intents are waiting in this 'work queue' ?

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65

2 Answers2

8

Actually It's quite easy: All you need to do is override onStartCommand(...) and increment a variable, and decrement it in onHandleIntent(...).

public class MyService extends IntentService {

 private int waitingIntentCount = 0;

 public MyService() {
  super("MyService");
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  waitingIntentCount++;
  return super.onStartCommand(intent, flags, startId);
 }


 @Override
 public void onHandleIntent(Intent intent) {
  waitingIntentCount--;
  //do what you need to do, the waitingIntentCount variable contains
  //the number of waiting intents
 }
}
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
1

Solution using SharedPreferences:

As per the documentation, the system calls onHandleIntent(Intent) when the IntentService receives a start request.

So, whenever you add an Intent to your queue, you increment and store an Integer that will represent the number of Intents in queue:

public void addIntent(){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    int numOfIntents = prefs.getInt("numOfIntents", 0);
    numOfIntents++;
    SharedPreferences.Editor edit = prefs.edit();    
    edit.putInt("numOfIntents",numOfIntents);
    edit.commit();
}

Then, each time onHandleIntent(Intent) is called you decrease that Integer value:

public void removeIntent(){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    int numOfIntents = prefs.getInt("numOfIntents", 0);
    numOfIntents--;
    SharedPreferences.Editor edit = prefs.edit();
    edit.putInt("numOfIntents",numOfIntents);
    edit.commit();
}

Finally, whenever you would like to check how many Intents that's in the queue, you simply fetch that value:

public void checkQueue(){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplication());
    int numOfIntents = prefs.getInt("numOfIntents",0);
    Log.d("debug", numOfIntents);
}
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • The problem is that `onHandleIntent` (did you mean `onStartCommand` ?) Is only called when the 'previous' `onHandleIntent` has finished. Either way, this is kind of a hack, is there no way to access the intent queue directly? Also, why store the integer in sharedPreferences, and not just a private (static ?) variable in the IntentService class? – Jonas Czech Feb 14 '15 at 15:57
  • I did a little research on this, and could not find any "official" way of achieving your goal. It could be a static variable, but then you could only access it from the Service class (I think), with `SharedPreference` you can access it from your whole application. @JonasCz – Marcus Feb 14 '15 at 16:00
  • I think you could replace `onHandleIntent` with `onStartCommand` – Marcus Feb 14 '15 at 16:01
  • Ok, found an answer [here](http://stackoverflow.com/questions/5357013/asking-an-intentservice-for-information-about-its-queue?lq=1). If I make the variable public, i will be able to access everywhere in may app, but I dont want this anyway. – Jonas Czech Feb 14 '15 at 16:07
  • 1
    One other problem with this answer: Your shared prefs data will persist - e.g., on device reboot - whereas your intents won't... – ban-geoengineering Feb 20 '16 at 10:54