0
votes

I have a BroadcastReceiver(#1) that runs all the time to catch an event. When the event occurs, it may need to make UI changes on a particular activity (if that activity is active). To do this, I have BroadcastReceiver#1 create a new intent that is caught by a second BroadcastReceiver(#2) that lives on the activity, and is is registered/unregistered only when the Activity is not paused...as discussed here: Inform Activity from a BroadcastReceiver ONLY if it is in the foreground

All that is working...however, now I want BroadcastReceiver#1 to post a status notification when the activity is not in focus OR when it is in focus but chooses not to handle the event. Thus, the Activity (if active) needs some way to communicate back to BroadcastReceiver#1.

I thought I would have BroadcastReceiver#1 spawn a thread that will post the event with a notification after X seconds have elapsed...where this thread will have a THIRD broadcastreceiver#3, and if the recieving activity chooses to handle the event, it can send a "dont bother posting that notification" intent which will be caught by BroadcastReceiver#3 in order to abort the notification. However, this does not work, because Android does not allow one BroadcastReceiver to register another BroadcastReceiver.

1

1 Answers

0
votes

Have never used it, but should work. Just before sending broadcast query whether any broadcast recievers are ready to handle your intent .

PackageManager pm = context.getPackageManager();
List<ResolveInfo>  mList = pm.queryBroadcastReceivers(intent, 0);

if(mList.size() > 0){
 //broadcast intent
}else{
 //post notification
}

Edit: Since this does not work on recievers registered through activites, you could track the register and unregister using Application class. Since same application and process is being used, you can make helper functtion to register and unregister and keep a count of registration and decide whether to post notification or broadcast an intent