I've been wondering if it is possible to use an IntentService
to do some networking while keeping the queue of pending intents prioritized. My goal to be able to download some images in the background, add more if needed (send another Intent
) and be able to reset the queue if necessary (preferably using a specific Intent). That is all possible with an IntentServie
but when I send that 'stop' Intent
it needs to be processed as the next item in the queue, not the last where it is right now.
EDIT
For those interested I have taken the AOSP code for IntentService
and modified it to meet my needs. The reason I cannot just subclass IntentHandler is because of the private ServiceHandler
class inside of IntentHandler
.
Inside of the ServiceHandler
I have a new method:
public final boolean sendPriorityMessage(Message msg)
{
int priority = msg.arg2;
//Log.i(GenericList.TAG,"recieved message priority: "+priority);
if(priority>PRIORITY_NORMAL){
return sendMessageAtFrontOfQueue(msg);
}else{
return sendMessage(msg);
}
}
This method is called from onStart
instead of just sendMessage
@Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
try{
msg.arg2 = intent.getExtras().getInt(KEY_PRIORITY);
}catch(Exception e){
msg.arg2 = PRIORITY_NORMAL;
}
mServiceHandler.sendPriorityMessage(msg);
}
Overall the code is still limited but I am able to fast track some messages to the front of the queue, which is what I was after anyway.