---
title: "give an example where you can use broadcast receiver?"  
description: "give an example where you can use broadcast receiver?"  
author: "Prateek sharma"  
published: 2018-04-07  
canonical: https://answers.mindstick.com/qa/40997/give-an-example-where-you-can-use-broadcast-receiver  
category: "android"  
tags: ["android"]  
reading_time: 2 minutes  

---

# give an example where you can use broadcast receiver? 

## Answers

### Answer by Arti Mishra

**[Broadcast](https://www.mindstick.com/articles/1633/broadcastreceiver-in-android) Receivers in [Android](https://www.mindstick.com/articles/1672/android-internals):**

![give an example where you can use broadcast receiver?](https://answers.mindstick.com/questionanswer/3f1145b3-6351-4108-997c-08fc67e1cb24/images/f24de958-c6c2-4c43-83ab-76f8dc8fa787.png)**\**

Broadcast Receiver is used to **broadcast a messages from one application to another applications** or our own system itself. For some time this message is called **intents or event**. And when any event occur then broadcast are sent. \
**Another example,** the Android system sends broadcasts when any types of events occur in your system, such as when the system starting the boots up and the device connect for charging. In this situation, apps can also **send custom broadcasts** to notify other apps are might be downloaded or uploaded. When a broadcast is send to any system, the s**ystem automatically routes broadcasts to your apps** that have subscribed to receive that particular type of broadcast. Generally speaking, broadcasts receiver can be **used as a messaging system** across apps and outside of the normal user flow. It is used to **perform jobs in the background** and can be contribute to a **slow system performance.****\**![give an example where you can use broadcast receiver?](https://answers.mindstick.com/questionanswer/3f1145b3-6351-4108-997c-08fc67e1cb24/images/b917f109-702a-406b-9b8d-2ef815e4d9a1.png)**\**[Image Source](https://www.mindstick.com/forum/34428/how-to-remove-image-source-from-html-content)**\**If you want to set up a Broadcast Receiver in your android app you need to do the following two things. 1. Creating a [BroadcastReceiver](https://www.mindstick.com/interview/2622/is-there-anyway-to-determine-if-an-intent-passed-into-a-broadcastreceiver-s-onreceive-is-the-result-of-a-sticky-boradcast-intent-or-if-it-was-just-sent) 2. Registering a BroadcastRecever\
\
**Creating a broadcast receiver in Android:** public class MyBroadcastReceiver extends BroadcastReceiver { public MyBroadcastReceiver () { } @Override [public void](https://www.mindstick.com/interview/2715/can-we-write-static-public-void-instead-of-public-static-void) onReceive(Context context, Intent intent) { Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_SHORT).show(); } } **Note:** When any event occurs then **onReceiver() method is first called for registered the Broadcast Receivers.****\** **Registering the Broadcast Receiver in Android:** A BroadcastReceiver can be registered in mainly two ways.\
**1. Firstly you can define the code in [AndroidManifest.xml file](https://www.mindstick.com/interview/2588/explain-androidmanifest-xml-file-in-detail) is as follows-** <receiver android:name=".ConnectionReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>\
**2. By Using Code (programmatically).****\** IntentFilter filter = new IntentFilter(); intentFilter.addAction(getPackageName()+"android.net.conn.CONNECTIVITY_CHANGE"); MyReceiver myReceiver = new MyReceiver(); registerReceiver(myReceiver, filter); For unregister a broadcast receiver you can call onStop() or onPause() method to [your activity](https://answers.mindstick.com/qa/33499/where-can-you-define-the-icon-for-your-activity-explain). @Override protected void onPause() { unregisterReceiver(myReceiver); super.onPause(); }\
**Sending the broadcast intent from an activity** Intent intent = new Intent (); intent.setAction("com.mindstick.CUSTOM_INTENT"); sendBroadcast(intent); \


---

Original Source: https://answers.mindstick.com/qa/40997/give-an-example-where-you-can-use-broadcast-receiver

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
