As we all know There are four types of components which can be used in Android, a good android developer should be Familiar with these components and the idea behind them in order to design and implement efficient applications.
I think it is safe to say that the most significant thing about android is the sharing philosophy on which it is based and understanding this concept could help us to have a more solid view on when and how to use each one of these component types.
Android applications can share their stuff and interact with each other and that is the main idea behind Content Providers,Services and Broadcast Receivers in Android: Sharing Data, Services and Events.
In this article I am gonna talk about Broadcast Receivers in Android and I will try to cover almost everything that we might need to know about them in different situations. Broadcast Receiver is actually a mechanism to send and receive events so that all interested applications can be informed when something happens. there are heaps of System events which get broadcast by Android OS such as SMS related events, Connectivity related events, Camera related events and many more.
We are able to broadcast our application specific events as well, so for example if we have a RSS news reader application and we want to do something whenever a new item is available, it would be a good idea to use Broadcast Receiver method, not only because it will separate your event handling code but most importantly because it will enable other applications to register and receive a notification whenever that event takes place.
The first thing we will be talking about is how to implement a Receiver and register it so that you can receive events that you want to receive. Implementing a Receiver is pretty simple and straight forward, all you need to do is to extend Broadcast Receiver class and override its onReceive() method. once you implement your Receiver you need to register it and determine what kind of events you are interested in to be sent to you.
For example if you want to get notified whenever there is an incoming call or SMS you will need to add something like this to you manifest file :
and here is CallAndSMSListener class source code :
In this example I have registered my Receiver in Manifest file, but we can also register a Receiver dynamically in our code like this :
in this case after calling registerReceiver our ConnectivityListener will be notified when Wi-Fi state or Connectivity state of mobile phone change. Just remember to unregister all Receivers that you have registered in your code using unregisterReceiver() method.
ConnectivityListener class looks like this :
So far we have seen how to register a Receiver both statically and dynamically,but it is just half the story; as i said we can not only receive events but can also broadcast our events either locally for just a specific Receiver or globally for all other applications.
In my application I have something like this :
As you can see i specified the class type for the Intent, so when i broadcast the Intent, only MyReceiver will be notified. if we wanted to globally broadcast it we should set an Action string for our Intent so that all receivers which have indicated that action in their intent filter would be notified.
this is what you need to put in your manifest file for this example:
and MyReceiver class:
Sometimes you dont want to broadcast an event directly from your activity but for whatever reason you want another application do that on your behalf, in these sort of cases you should use PendingIntent class instead of Intent. AlarmManager class is a good example to illustrate this kind of scenarios, AlarmManager class allows you to set a time to alarm system goes off, you can also specify a PendingIntent object to be broadcast when alarm goes off.
I have used this piece of code in my Activity onStop() method :
It means that alarm goes off 4 hours after each time we close the application and when it happens it also broadcast the specified intent it has been provided earlier.
and here is the content of Reciever's onRecieve() method :
so simple, it just starts our activity again. 4 hours after that we close the application the alarm goes off and the application comes up again.
when we send an ordinary broadcast all Registered Receivers will get notified but the order in which they are notified is undefined, in other words you wouldn't know which Receiver is called first and which one last. another thing to remember is you are not allowed to stop the propagation of an event in a Receiver. but what if you really need to be able to stop the propagation of an event in an Receiver so that other receivers dont get it or what if you need to set an order according to which Receivers should be called, thankfully it's not a big deal, all we would need to do is to call sendOrderedBroadcast() method instead of sendBroadcast() to solve the problem.
For example I have used this code in my application :
I also have two receivers in my manifest file :
When we call OrderedBroadcast() method we have two registered Receiver which need to be called, but as you can see we have used android:priority attribute for them, it means that Receiver with higher value will be called first, in this example FirstReciever is notified first and then SecondReciever get notified if FirstReciever have not aborted the intent.
LastReciever is always gonna get notified regardless of what has happened in other Receivers.
and here is these three classes source codes :
Do not forget to add these Permission if you want to test these examples: