Broadcast receiver class
In Android Development, the BroadcastReceiver class is a fundamental component that enables communication between different parts of an application or between different applications on an Android device. It allows the application to listen for and respond to broadcast messages sent by the system or other applications. This class is primarily used to handle events or notifications in an asynchronous manner.
Use Case:
The BroadcastReceiver class is commonly used in scenarios where an application needs to respond to system events, such as when the device boots up, the network connectivity changes, the battery level changes, an SMS message is received, or a notification is posted. By registering a BroadcastReceiver, the application can be notified of these events and perform specific actions in response.
There are mainly two types of Broadcast Receivers:
Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed but due to the security reasons this is stoped from API Level 26.
Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.
Following are some of the important system-wide generated intents:-
android.intent.action.BATTERY_LOW
Indicates low battery condition on the device.
android.intent.action.BATTERY_OKAY
Indicates the battery level has returned to normal.
android.intent.action.SCREEN_ON
Indicates the device's screen has turned on.
android.intent.action.SCREEN_OFF
Indicates the device's screen has turned off.
android.intent.action.ACTION_POWER_CONNECTED
Indicates the device is connected to a power source.
android.intent.action.ACTION_POWER_DISCONNECTED
Indicates the device is disconnected from a power source.
android.intent.action.ACTION_BOOT_COMPLETED
Indicates the device has completed the boot process.
android.intent.action.TIME_TICK
Indicates a minute has passed and the system clock has changed.
How to create a BroadcastReceiver class?
To create a BroadcastReceiver class, you need to create a class that extends the BroadcastReceiver class and override its onReceive()
method. This method will be called whenever a broadcast message is received by the device. You can then perform your desired actions in this method.
How to register a BroadcastReceiver?
To use the BroadcastReceiver class, you need to register it either dynamically or statically in your AndroidManifest.xml file. Here's an example of registering the receiver dynamically:
Once the receiver is registered, whenever a broadcast message is received by the device, the onReceive()
method of MyReceiver
will be invoked. You can then perform your desired actions, such as displaying a notification, processing the message, or updating the UI.
How to unregister a BroadcastReceiver?
To unregister a BroadcastReceiver, you need to call the unregisterReceiver()
method and pass the BroadcastReceiver object as an argument. For example:
How to send a broadcast message?
To send a broadcast message, you need to create an Intent object and pass the action name as an argument. For example:
You can also pass additional data with the Intent object. For example:
Example:
Let's consider an example where you want to build an application that responds to incoming SMS messages. step1: create a class that extends the BroadcastReceiver class and override its onReceive()
method. This method will be called whenever an SMS message is received by the device. SMSReceiver.java
step2: To use the SMSReceiver
class, you need to register it either dynamically or statically in your AndroidManifest.xml file. Here's an example of registering the receiver dynamically:
Once the receiver is registered, whenever an SMS message is received by the device, the onReceive()
method of SMSReceiver
will be invoked. You can then perform your desired actions, such as displaying a notification, processing the message, or updating the UI.
step3: You also need the permission to read user incomming sms messages. To request permissions at runtime in an Android application, you need to follow these steps:
A. Declare the permissions in the AndroidManifest.xml file, as mentioned in the previous response.
B. Check if the required permissions are granted or not before performing any actions that require those permissions. You can use the checkSelfPermission()
method to check the status of a permission. For example:
C. Implement the onRequestPermissionsResult()
method in your activity to handle the permission request result:
When you call requestPermissions()
, the system will display a dialog to the user asking for permission. The user can choose to grant or deny the permission. Once the user responds to the permission request, the onRequestPermissionsResult()
method will be called with the result. You can handle the result based on whether the permission was granted or denied.
Remember to replace MY_PERMISSIONS_REQUEST_RECEIVE_SMS
with your desired request code. This code is used to identify the specific permission request in the onRequestPermissionsResult()
method.
step4: Remember to unregister the receiver when you no longer need to listen for SMS messages:
This is just one example of how the BroadcastReceiver class can be used in Android Java. It allows you to listen for a wide range of system or application events and respond to them accordingly.
localBroadcastManager
The LocalBroadcastManager
class is used to send and receive broadcasts within your application. It is similar to the BroadcastReceiver
class, but it is more efficient and secure. It is recommended to use LocalBroadcastManager
instead of BroadcastReceiver
whenever possible.
To use the LocalBroadcastManager
class, you need to follow these steps:
Define a
BroadcastReceiver
class to handle the broadcast:
Register the
BroadcastReceiver
in your activity or fragment:
Send a local broadcast from any part of your code:
note: Remember to add the necessary permissions and register the activity in the manifest file.
LocalBroadcastManager and BroadcastReceiver
BroadcastReceiver
:A class that allows your application to receive and handle broadcast intents.
It can be registered either dynamically in code or statically in the manifest file.
It receives broadcasts based on the intent filters specified during registration.
It overrides the
onReceive()
method to define the actions to be taken when a broadcast is received.
LocalBroadcastManager
:A helper class that allows you to send and receive broadcasts within your application.
It operates within the application's process and is not visible to other applications.
It provides a mechanism for efficient communication between components within the same application.
It simplifies the process of sending and receiving broadcasts by limiting the scope to the local application.
A comparison between LocalBroadcastManager
and BroadcastReceiver
:
Scope
Restricted to the local application
Can be used across applications
Broadcasting
Can send broadcasts within the application
Does not send broadcasts itself
Receiving
Can receive broadcasts within the application
Receives broadcasts from any sender
Communication
Efficient communication within the same application
Enables communication between different applications
Registration
No need to register in the manifest file
Can be registered in the manifest file
Dependency
Requires the LocalBroadcastManager class
Does not depend on the LocalBroadcastManager class
Customization
Provides limited customization options
Offers more flexibility in customization
Last updated
Was this helpful?