What is Android Broadcast Receiver?
A broadcast receiver is an Android application component which allows you to register for system generated events or application events. All registered broadcast receiver for a particular event are notified by the Android runtime once the registered event fires.
The Broadcast Receiver’s job is to pass a notification to the user when a specific event occurs.
Using a Broadcast Receiver, applications can register for a particular type of event. Once the event occurs, the system will notify all the registered applications in the system.
For example, applications can register for the ACTION_BOOT_COMPLETED system event which is fired once the Android system has completed the boot process.
The following table lists a few important system events. These events are defined as final static fields in the Intent class.
Sr.No | Event & Description |
1 | android.net.conn.CONNECTIVITY_CHANGE
The mobile network or wifi connection is changed. |
2 | android.intent.action.BATTERY_LOW
Indicates low battery condition |
3 | android.intent.action.BATTERY_OKAY
Indicates battery is now okay after the battery low condition. |
4 | android.intent.action.BOOT_COMPLETED
Indicates that device isfinished booting. |
5 | android.intent.action.DATE_CHANGED
Indicates that date has changed. |
6 | android.intent.action.REBOOT
Indicates that device is rebooted. |
The two important steps to make BroadcastReceiver to works for the system generated events or intents are
Step 1: Creating the Broadcast Receiver.
Step 2: Registering Broadcast Receiver.
In the case of a custom intents (events generated by user), you will have to create and broadcast those intents manually.
Creating the Broadcast Receiver
In order to implement broadcast receiver , the broadcast receiver class must be subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter as shown below.
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Custom Intent Detected.", Toast.LENGTH_LONG).show(); } }
Here I have created a java class MyReceiver as the broadcast receiver class.
Registering Broadcast Receiver
Next step is registering our broadcast receiver class named MyReceiver class in AndroidManifest.xml file. Here we are going to register MyReceiver for custom intent com.codeunplug.CUSTOM_INTENT which is fired by the system once you click on the button.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcastreceiver"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="MyReceiver"> <intent-filter> <action android:name="com.codeunplug.CUSTOM_INTENT"> </action> </intent-filter> </receiver> </application> </manifest>
Now whenever you click on the button, it will be intercepted by BroadcastReceiver MyReceiver and the code inside onReceive() method will be executed.
Here I have created a button named “BROADCAST INTENT”,when you click on the button the custom intent is thrown by using sendBroadcast(intent)metho and handled by the MyReceiver class. The overridden method onReceive() in the MyReceiver class will catch the event and toast a message “Custom Intent Detected”.
The layout for creating the button is shown below.
The layout design for the above image is shown below.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Example Of Broadcast Receiver for Custom Intent" android:textColor="@color/colorAccent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" android:gravity="center_horizontal"/> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="https://codeunplug.com/" android:textColor="#ff87ff09" android:textSize="30dp" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="Broadcast Intent" android:layout_centerHorizontal="true" android:onClick="broadcastIntent" android:layout_centerVertical="true" /> </RelativeLayout>
The corresponding java class for creating the custom intent is shown below.
MainActivity.java
package com.example.broadcastreceiver; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // broadcast a custom intent. public void broadcastIntent(View view){ Intent intent = new Intent(); intent.setAction("com.codeunplug.CUSTOM_INTENT"); sendBroadcast(intent); } }
Create a BroadcastReceiver class for receiving the custom intent. I have just created a class named MyReceiver.
MyReceiver.java
package com.example.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Custom Intent Detected.", Toast.LENGTH_LONG).show(); } }
Output :
When you click on the button,custom intent is fired and catch by the MyReceiver class. A toast message is displayed in the onReceive() method.