0

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.

Community
  • 1
  • 1
Stu
  • 1,999
  • 1
  • 14
  • 11

1 Answers1

0

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

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • Unfortunately this doesn't actually solve the original problem, because even though the activity is active and ready to receive the broadcast does not mean that it will handle it. In this case, the activity is a chat window...and if you are currently chatting with that person, then the activity will handle the event. Otherwise, if the chat activity is open with a different person, then I still want a status notification... – Stu Sep 15 '12 at 21:58
  • Actually, I realized that because the intent filter is just a String, I can combine the usual generic filter string with the specific ID...and then your method would seem to be valid. However, on testing it out, it seems that queryBroadcastReceivers only returns receives that registered statically in the manifest...and does not count dynamically registered broadcast receivers... – Stu Sep 15 '12 at 22:40
  • See also here: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/x34PX5hYFws – Stu Sep 15 '12 at 22:51