AndroidDev
AndroidDev

Reputation: 16405

Service sending multiple broadcasts Android

I have an activity that offloads multiple uploads to a Intent Service. Each upload has its own IntentService that naturally get executed one by one. My Activity has a Broadcast Receiver like this

public class ResponseReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        int position=intent.getExtras().getInt(POSITION); 
        long bytes=intent.getExtras().getLong(BYTES); 


    }
}

This is the message recieved from the IntenetService regarding the number of bytes upload. I use this information to update the progress bar information corresponding to the upload. This is all fine. My activity registers for a IntenetReciever as such

IntentFilter filter = new IntentFilter(ACTION_RESP);
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    receiver = new ResponseReceiver();
    registerReceiver(receiver, filter);

Now my question is that i want my Service to broadcast two messages. One when the onHandleIntent method begins to execute, and the second the number of bytes uploaded. The above filter is for the number of bytes uploaded. How can I register my activity for multiple broadcast messages from a same IntentService.

Upvotes: 0

Views: 752

Answers (1)

pcu
pcu

Reputation: 2735

In your activity you can register two receivers with different IntentFilter-s. Or you can have only one but then you need to distinguish different messages in your onReceive method.

@Override
public void onReceive(Context context, Intent intent)
{

    if(intent.getAction().equals("aaa")  {
        // do something here
    } else if (intent.getAction().equals("bbb")) {
        // do something different here        
    }


}

Upvotes: 1

Related Questions