Broadcast receiver in android with easy explanation

Today we will learn about the Android broadcast receiver.This is a very important framework for android application development.

Android Broadcast Receiver

Android Broadcast Receiver listens the broadcast message from the other application or system events or intent.

Unlike activity,it does not contain any user interface

It works similar to publisher-subscriber design patterns used for inter-communication process.

Example:

Application can be registered for different events such as low battery,boot completed.

There are following some system generated intent class

1.android.intent.action.BATTERY_LOW: This indicates the battery is low on device.

2.android.intent.action.BATTERY_CHARGED:This indicates the battery has charged.

3.android.intent.action.REBOOT:This indicates the reboot device.

4.android.intent.action.BOOT_COMPLETED:This indicates the boot has completed.

5.android.intent.action.DATE_CHAGED:This indicates the date has changed.

6.android.intent.action.BUG_REPORT:This indicates the reporting bugs

Broadcast Receiver

There are two steps to implement broadcast receiver in android application.

1.Creating Broadcast Receiver

2.Registering Broadcast Receiver

1.Creating Broadcast Receiver

For receiving broadcast we need   extends BroadcastReceiver class and override the onReceive( ) method where each message is received with the help of intent.

public class MyReceiver extends BroadcastReceiver
@Override
Public void onReceive(Context context,Intent intent)
{
Toast.makeText(context,”broadcast message”,Toast.LENGTH_LONG).show( );
}

2.Registering broadcast receiver

1.Manifest declaration

Here i am registering the android.intent.action.BATTERY_LOW  inside the AndroidManifest.xml.

<application>
<receiver android:name=”MyBroadcastReceiver”>
<intent-filter>
<action android:name=”android.intent.action.BATTERY_LOW”>
</action>
</intent-filter>
</receiver>
</application>

2.Context-registered

Registering the receiver dynamically via Context.registerReceiver( ) method.

When the battery is low then broadcast message will execute inside the Broadcast class inside the onReceive( ) method.

Send Broadcast intents  from the Activity/Custom Intent

It is used to send intent to all the broadcast receiver and these intents can be created using the sendBroadcast( )  method inside the activity.

The snippet  is given below:

Public void broadcastIntent(View view)
{
Intent intent=new Intent( );
intent.setAction(“com.placement.CUSTOM_INTENT”);
sendBroadcast(intent);
}

Also add the below code inside the  AndroidManifest.xml.

<application>
<receiver android:name=”MyReceiver”>
<intent-filter>
<action android:name=”com.placement.CUSTOM_INTENT”>
</action>
</intent-filter>
</receiver>
</application>

Conclusion


I hope I have explained all the steps very clearly.if you have any query then you can comment me.

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *