articles

Home / DeveloperSection / Articles / Broadcast receiver in Android

Broadcast receiver in Android

Manoj Pandey3198 21-Oct-2015

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.app.ActivityManager;

import android.app.ActivityManager.RunningServiceInfo;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.os.StrictMode;

 

import com.mindstick.mchat.GlobalClass;

 

public class NetworkChangeReceiver extends BroadcastReceiver {

 

     static Intent intentService;

     static Context mcontext;

    

 

     @Override

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

          // TODO Auto-generated method stub

          mcontext = context;

          if (isNetworkAvailable(context)) {

 

       Toast.makeText(context,

                "Network Connection is available", Toast.LENGTH_SHORT)

                             .show();

                  

 

          } else {

        Toast.makeText(context,

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

                             .show();

         

          }

     }

 

    

 

     public static boolean isNetworkAvailable(Context context) {

          boolean isMobile = false, isWifi = false;

      ConnectivityManager connectivity = (ConnectivityManager) context

                   .getSystemService(Context.CONNECTIVITY_SERVICE);

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,

                                      NetworkChangeReceiver.class);

                             sendBroadcast(intentBroadCast);

 

3.    Define broadcast in manifest.xml file

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

    <intent-filter>

     <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

            </intent-filter>

        </receiver>

 

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

<uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="18" />

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


 5.   Now run your application

     Broadcast receiver in Android


Updated 07-Sep-2019

Leave Comment

Comments

Liked By