views:

37

answers:

1

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.

A: 

you could implement/extend your own PriorityQueue that simply checks every new intent added to the queue. if it's the stop intent, it moves it straight to the front of the line.

mtmurdock
The PriorityQueue by itself is just the data structure though, What I need is IntentService that uses a PriorityQueue to store pending intents. It might be possible if I were to subclass and override the corresponding methods. Time to dig though some of Android's source code...
smith324