Hello everyone. We will speak about broadcast receiver in the Android Devices in this article.
Broadcast receiver is a messaging system used on Android Devices. But this messaging system is used between the Android Operating System and Android Applications. Android Operating System shares device-related information with Android Applications.
This device-related information can be device battery level, is device connected to the wi-fi or not, device starts to charging, is device in airplane mod or not, and etc.
If the application wants to know one of these information, it will subscribe to the operating system to get this information. When these system events occur, operating system sends broadcast message to the subscribed applications.
Android Operating System sends these messages when these system events occur. This is important. Because whenever one of the system events occurs, Android Operating System sends broadcast messages in that time.
For example, if you want your application to learn when the device battery is low, you will subscribe your application to that specific system event. And, when the device battery is low, Android Operating System will send a broadcast message to your application.
Or, if you want your application to know when the device connected to the wi-fi, you can subscribe your application to that system event. And, whenever the device connected to the wi-fi, Android Operating System will send a broadcast message to your application.
When your Android Device gives a warning to you that battery is low, it also sends this message to all of the subscribed applications.
Also, when the Android Device switches to airplane mode, system sends this message to all of the subscribed applications.
So, how will be useful to learn this information for the Android Applications?
Our application subscribes to the information we want. Android Operating System does not send every information or system events. We subscribe to system events which one of we need in our application.
So, we take information from the system. Where will we use this information?
For example, you develop an application. This application saves user photo in the server. such an application allows the user to access their photos from anywhere. This is a nice feature.
But how will you send photos to the server? If you send every photo to the server when the photo was taken, it will finish the user mobile data. Because the user can take hundreds of photos. This is not a good feature for an application. Nobody wants to use this kind of application.
You should save photos in the device memory and when the device connected to the wi-fi, you can send photos to the server. This kind of feature will reduce the user’s mobile data consumption.
So, if you want to develop this kind of application, you need to learn when the device connected to the wi-fi. Your application can subscribe to the system events and when the device connected to the wi-fi, Android Operating System sends a broadcast receiver message to the application. after this message, your application starts to send user photos to the server.
Also, when the device disconnected to the wi-fi, the system sends a message again to the application and your application stops to send photos to the server.
So, Where else can we use the broadcast receiver?
For example, applications that verify by message can use the broadcast messaging system. You know many applications are using message validation system such as WhatsApp, Viber. And some of them, when the message comes, read the message automatically.
You can develop this kind of application using the broadcast messaging system. Your application subscribes to the system event and when the new message comes to the phone, your application will know this. After that, your application can read this message automatically.
I can give another example. When the device battery is low, the system sends a message to the subscribed applications. After takes this message, applications can stop its background tasks. This both reduces device battery consumption and it prevents sudden interruption of the background task.
When the battery level is normal, the system sends another message to the applications. After this message, applications can restart background tasks.
The use of these broadcast messages when designing the application is important for the more efficient and better quality of the application.
Also, I want to mention this, the broadcast messaging system can be used between two applications.
That means, one application can send a broadcast message to the other application. But, most of the time, it is used between the Android Operating System and Android Applications.
So, I will finish this article here. See you in the next article.