Android

Android Broadcast Receivers

Android Broadcast Receivers

Broadcast receivers are used to respond to the broadcast messages received from other applications. To create Broadcast Receiver in Android, you need to implement two steps that are described below:

Step – 1: Create a Broadcast Receiver
To create a broadcast receiver, BroadcastReceiver class is used with a subclass and onReceive() method is overridden. See the following code for reference:

public class BroadReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Broadcast received.", Toast.LENGTH_LONG).show();
   }
}

Step – 2: Registration of Broadcast Receiver
Now we are going to register the broadcast receiver we just created. To do that, we have to register a broadcast receiver in our manifest.xml file. After registration, the manifest.xml file will look like this:

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="BroadReceiver">
      <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED">
         </action>
      </intent-filter>
   </receiver>
</application>

This way, every time the Android device is turned on, it will be obstructed by Broadcast Receiver ‘BroadReceiver’ and the logic is implemented under the onReceive() method that gets executed after an interception by the broadcast receiver. 
 

Top course recommendations for you

    Circular Queue
    1 hrs
    Beginner
    3.1K+ Learners
    4.5  (213)
    Python Stack
    2 hrs
    Beginner
    5.8K+ Learners
    4.45  (301)
    AI and Big Data in IOT
    1 hrs
    Intermediate
    10.2K+ Learners
    4.47  (1152)
    Selenium Basics
    1 hrs
    Beginner
    25.4K+ Learners
    4.43  (1917)
    Selenium with Python
    1 hrs
    Beginner
    13.2K+ Learners
    4.44  (819)
    Selenium Projects with Python
    2 hrs
    Intermediate
    8.8K+ Learners
    4.5  (242)
    Java Projects
    1 hrs
    Beginner
    25.2K+ Learners
    4.19  (243)
    JDBC in Java
    1 hrs
    Beginner
    9.3K+ Learners
    4.42  (797)
    Flask Python
    1 hrs
    Beginner
    8.2K+ Learners
    4.38  (456)
    Linked List in Python
    3 hrs
    Beginner
    2.9K+ Learners
    4.55  (62)