0% found this document useful (0 votes)
7 views3 pages

Broadcast Receiver

Broadcast receivers are a core Android component that allows applications to send and receive messages, known as broadcasts, from the system or other applications. They can be registered either statically in the manifest file or dynamically using the Context.registerReceiver() method, with specific guidelines for when to unregister them to avoid memory leaks. There are three methods to send broadcasts: sendOrderedBroadcast, sendBroadcast, and LocalBroadcastManager.sendBroadcast, each with different behaviors regarding message delivery.

Uploaded by

Azharr Nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Broadcast Receiver

Broadcast receivers are a core Android component that allows applications to send and receive messages, known as broadcasts, from the system or other applications. They can be registered either statically in the manifest file or dynamically using the Context.registerReceiver() method, with specific guidelines for when to unregister them to avoid memory leaks. There are three methods to send broadcasts: sendOrderedBroadcast, sendBroadcast, and LocalBroadcastManager.sendBroadcast, each with different behaviors regarding message delivery.

Uploaded by

Azharr Nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Broadcast receiver are one of the four core android components.

Simply, Broadcast Receivers can send or receive messages from


other applications or from the system itself. These messages
can be events or intents. For instance, Android system sends
broadcasts when system events occur such as system boots up,
device starts charging, connetivity chaning, date chaning, low
battery. Furthermore, apps can send custom broadcasts to notify

other apps(data download completed).

Apps can receive broadcasts in two ways:

1-Manifest-declared receivers(Statically)

2-Context-registered receivers(Dynamically)

Manifest-declared receivers(Statically)
The registration is done in the manifest file, using <register> tags.
Firstly, specify the receiver in your manifest file.
<receiver android:name=".MyBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE""/>

</intent-filter>
</receiver>

Secondly, create class as a subclass of BroadcastReceiver class and


overriding the onReceive() method where each message is received
as a Intent object parameter.
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.",
Toast.LENGTH_LONG).show();
}
}

Context-registered receivers(Dynamically)
The registration is done
using Context.registerReceiver() method.

Firstly, register broadcast receiver programmatically.


IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(getPackageName() +
"android.net.conn.CONNECTIVITY_CHANGE");

MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();


registerReceiver(myReceiver, filter);

Secondly, To unregister a broadcast receiver in onStop() or


onPause() of the activity.
@Override protected void onPause() {
unregisterReceiver(myReceiver);
super.onPause(); }

Be mindful of where you register and unregister the receiver, for


example, if you register a receiver in onCreate(Bundle) using the
activity's context, you should unregister it in onDestroy() to prevent
leaking the receiver out of the activity context. If you register a
receiver in onResume(), you should unregister it in onPause() to
prevent registering it multiple times (If you don't want to receive
broadcasts when paused, and this can cut down on unnecessary
system overhead). Do not unregister
in onSaveInstanceState(Bundle), because this isn't called if the user
moves back in the history stack.

Sending broadcasts
There are three distinctive way to send broadcast:

1- The sendOrderedBroadcast(Intent, String) method sends broadcasts to


one receiver at a time.

2-The sendBroadcast(Intent) method sends broadcasts to all receivers


in an undefined order.

3-The LocalBroadcastManager.sendBroadcast method sends broadcasts to


receivers that are in the same app as the sender.

You might also like