Broadcasts in Android

 Android Broadcast Receiver is one of the principal components of Android application development. With this component we can register receivers for any system-level or application-level event. Once that event occurs, android system will notify the registered receivers about the execution of events respectively.

Types of Broadcasts

There are two types of broadcasts received by receivers and they are:

1. Normal Broadcasts:

·         These are asynchronous broadcasts.

·         Receivers of this type of broadcasts may run in any order, sometimes altogether.

·         This is efficient.

·         Receivers cannot use the result.

·         They cannot abort the included APIs.

·         These broadcasts are sent with Context.sendBroadcast

2. Ordered Broadcasts

·         These are synchronous broadcasts.

·         One broadcast is delivered to one receiver at a time.

·         Receivers can use the result. In fact as each receiver executes, result is passed to next receiver.

·         Receiver can abort the broadcast and hence no broadcast is received by other receivers.

·         The order of receivers is managed and controlled by the attribute android:priority in corresponding intent-filter.

·         If receivers will have same priority then they may run in any order.

Broadcasts are messaging components used for communicating across apps when events of interest occur. There are two types of broadcasts:

·         System broadcasts are delivered by the system.

·         Custom broadcasts are delivered by your app.

System broadcasts

system broadcast is a message that the Android system sends when a system event occurs wrapped in in Intent objects.

android.intent.action.HEADSET_PLUG

The intent can also contain more data about the event in its extra field, for example a boolean extra indicating whether a headset is connected or disconnected.

 

Examples:

When the device boots, the system broadcasts a system Intent with the action ACTION_BOOT_COMPLETED.

When the device is disconnected from external power, the system sends a system Intent with the action field ACTION_POWER_DISCONNECTED.

 

Custom broadcasts

Custom broadcasts are broadcasts that our app sends out. Use a custom broadcast when we want our app to take an action without launching an activity. For example, use a custom broadcast when we want to let other apps know that data has been downloaded to the device and is available for them to use. More than one broadcast receiver can be registered to receive your broadcast.

To create a custom broadcast, define a custom Intent action.

When you specify the action for the Intent, use your unique package name (for example com.example.myproject) to make sure that your intent doesn't conflict with an intent that is broadcast from a different app or from the Android system.

There are three ways to deliver a custom broadcast:

·         For a normal broadcast, pass the intent to sendBroadcast().

·         For an ordered broadcast, pass the intent to sendOrderedBroadcast().

·         For a local broadcast, pass the intent to LocalBroadcastManager.sendBroadcast().


Normal broadcasts

The sendBroadcast() method sends broadcasts to all the registered receivers at the same time, in an undefined order. This is called a normal broadcast. A normal broadcast is the most efficient way to send a broadcast. With normal broadcasts, receivers can't propagate the results among themselves, and they can't cancel the broadcast.

The following method sends a normal broadcast to all interested broadcast receivers:

public void sendBroadcast() {

   Intent intent = new Intent();

   intent.setAction("com.example.myproject.ACTION_SHOW_TOAST");

   // Set the optional additional information in extra field.
   intent.putExtra("data","This is a normal broadcast");
   sendBroadcast(intent);
}

Ordered broadcasts

To send a broadcast to one receiver at a time, use the sendOrderedBroadcast() method:

·         The android:priority attribute that's specified in the intent filter determines the order in which the broadcast is sent.

·         If more than one receiver with same priority is present, the sending order is random.

·         The Intent is propagated from one receiver to the next.

·         During its turn, a receiver can update the Intent, or it can cancel the broadcast. (If the receiver cancels the broadcast, the Intent can't be propagated further.)

For example, the following method sends an ordered broadcast to all interested broadcast receivers:

public void sendOrderedBroadcast() {
   Intent intent = new Intent();
 
   // Set a unique action string prefixed by your app package name.
   intent.setAction("com.example.myproject.ACTION_NOTIFY");
   // Deliver the Intent.
   sendOrderedBroadcast(intent);
}

Local broadcasts

If you don't need to send broadcasts to a different app, use the LocalBroadcastManager.sendBroadcast() method, which sends broadcasts to receivers within your app. This method is efficient, because it doesn't involve interprocess communication. Also, using local broadcasts protects your app against some security issues.

To send a local broadcast:

1.       To get an instance of LocalBroadcastManager, call getInstance() and pass in the application context.

2.       Call sendBroadcast() on the instance. Pass in the intent that you want to broadcast.

 LocalBroadcastManager.getInstance(this).sendBroadcast(customBroadcastIntent);

 


Comments

Popular posts from this blog

Android - Using KeyStore to encrypt and decrypt the data

Stack and Queue

Java Reflection API