---
title: "Broadcast receiver in Android"  
description: "Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events"  
author: "Manoj Pandey"  
published: 2015-10-21  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/11882/broadcast-receiver-in-android  
category: "android"  
tags: ["android", "broadcast"]  
reading_time: 2 minutes  

---

# Broadcast receiver in Android

Broadcast Receivers simply respond to broadcast [messages](https://www.mindstick.com/news/4179/google-faces-backlash-for-claiming-all-texts-in-google-messages-are-end-to-end-encrypted) from other [applications](https://www.mindstick.com/articles/12847/how-to-choose-the-right-ethernet-cable-for-industrial-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](https://answers.mindstick.com/qa/40997/give-an-example-where-you-can-use-broadcast-receiver) who will intercept this [communication](https://www.mindstick.com/articles/126321/5-fails-and-fixes-of-the-office-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](https://www.mindstick.com/interview/2588/explain-androidmanifest-xml-file-in-detail).

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](https://answers.mindstick.com/qa/33390/how-to-enable-api-access-in-salesforce-by-permission-set) 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](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding)

![Broadcast receiver in Android](https://www.mindstick.com/mindstickarticle/59cfb6ed-6729-4834-915f-4c0019541cac/images/d97ba360-f775-4ae3-bc3c-1dd64fc86a46.png)

---

Original Source: https://www.mindstick.com/articles/11882/broadcast-receiver-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
