Broadcast receiver in Android

Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.

There are two ways to register Android broadcastreceiver.

One is static way in which the broadcast receiver is registered in an android application via AndroidManifest.xml file.

Another way of registering the broadcast receiver is dynamic, which is done using Context.registerReceiver( ) method. Dynamically registered broadcast receivers can be unregistered using Context.unregisterReceiver( ) method.

Here I am crating a sample to broadcast on change device data connection

1.      Create a class and this class extends with BroadCastReceiver





import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;



import android.os.StrictMode;


import com.mindstick.mchat.GlobalClass;


public class NetworkChangeReceiver extends BroadcastReceiver {


     static Intent intentService;

     static Context mcontext;




     public void onReceive(final Context context, Intent intent) {

          // TODO Auto-generated method stub

          mcontext = context;

          if (isNetworkAvailable(context)) {



                "Network Connection is available", Toast.LENGTH_SHORT)




          } else {


         "Network Connection is not available", Toast.LENGTH_SHORT)








     public static boolean isNetworkAvailable(Context context) {

          boolean isMobile = false, isWifi = false;

      ConnectivityManager connectivity = (ConnectivityManager) context


NetworkInfo[] infoAvailableNetworks = connectivity.getAllNetworkInfo();


          if (infoAvailableNetworks != null) {

              for (NetworkInfo network : infoAvailableNetworks) {


               if (network.getType() == ConnectivityManager.TYPE_WIFI) {

                   if (network.isConnected() && network.isAvailable())

                             isWifi = true;


             if (network.getType() == ConnectivityManager.TYPE_MOBILE) {

                   if (network.isConnected() && network.isAvailable())

                             isMobile = true;





          return isMobile || isWifi;





2.     Call this broadcast on MainActivity.class

Intent intentBroadCast = new Intent(MainActivity.this,




3.    Define broadcast in manifest.xml file

<receiver android:name="com.example.demoBroadCast.NetworkChangeReceiver" >


     <action android:name="" />




4.    Add permission in manifest.xml file to access network state 



        android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 5.   Now run your application

     Broadcast receiver in Android

  Modified On Sep-07-2019 12:15:58 AM

Leave Comment