Android Broadcast Receivers Tutorial

Android Broadcast Receivers

Android Broadcast Receivers

A summary of Broadcast Receivers

In this tutorial we will cover android broadcast receivers. An application listens for particular broadcast intents by registering a broadcast receiver. Broadcast receivers are put into practice by extending the Android BroadcastReceiver class and overriding the onReceive() technique. The broadcast receiver may then be indexed, either within code (for instance within an activity), or within a manifest file. A section of the indexing implementation involves the formation of intent filters to specify the particular broadcast intents the receiver is needed to listen for. This is achieved by referencing the action string of the broadcast intent.

When a matching broadcast is perceived, the onReceive() technique of the broadcast receiver is called, at which point the technique has 5 seconds within which to carry out any essential tasks before returning. It is significant to note that a broadcast receiver does not require to be running all the time. In the event that a matching intent is noticed, the Android runtime system will routinely start up the broadcast receiver prior to calling the onReceive() technique.

The following code lists a template Broadcast Receiver subclass:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
  // Implement code here to be performed when
  // broadcast is detected
 }
}

When registering a broadcast receiver within a marked file, a <receiver> entry ought to be added containing one or more intent filters, all made up of the action string of the broadcast intent for which the receiver is needed to listen.

The following example shows file registers the above example broadcast receiver to listen for broadcast intents made up of an action string of com.example.Broadcast:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.BroadcastDetector"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="10" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name="MyReceiver" >
            <intent-filter>
                <action android:name="com.example.Broadcast" >
                    </action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

The equivalent effect can be accomplished by registering the broadcast receiver in code with the use of the registerReceiver() technique of the Activity class in addition to a suitably configured IntentFilter object:

IntentFilter filter = new IntentFilter(“com.example.Broadcast”);

MyReceiver receiver = new MyReceiver();

registerReceiver(receiver, filter);

When a broadcast receiver indexed in code is no longer needed, it may be unregistered through a call to the unregisterReceiver() technique of the activity class, moving through a reference to the receiver object as an argument. For instance, the following code will unregister the above broadcast receiver:

unregisterReceiver(receiver);

It is essential to bear in mind that a few system broadcast intents can only be detected by a broadcast receiver if it is registered in code instead of in the manifest file.

Obtaining Results from a Broadcast

When a broadcast intent is sent  with the use of the sendBroadcast() method, there is no way for the starting activity to receive results from any broadcast receivers that accepts the broadcast. In the event that return results are needed, it is rather essential to make use of the sendOrderedBroadcast() technique. When a broadcast intent is sent with this technique, it is sent in sequential order to each broadcast receiver with a registered interest.

The sendOrderedBroadcast() technique is called with a number of arguments including a reference to another broadcast receiver (referred to as the result receiver) which is to be notified when every other broadcast receivers have taken care of the intent, in addition to a collection of data references into which those receivers can put result data. When all broadcast receivers have been offered the opportunity to take care of the broadcast, the onReceive() method of the result receiver is called and passed the result data.

 Sticky Broadcast Intents

By default, broadcast intents fade away once they have been sent and taken care of by any interested broadcast receivers. A broadcast intent can, nevertheless, be defined as being “sticky”. A sticky intent, and the data contained in that, remains current in the system after it has accomplished. The data stored within a sticky broadcast intent can be gotten through the return value of a call to the registerReceiver() technique, with the use of the usual arguments (references to the broadcast receiver and intent filter object). A lot of the Android system broadcasts are sticky, the major example are those broadcasts that are connected to battery level status.

A sticky broadcast may be removed at anytime via a call to the removeStickyBroadcast() method, passing through as an argument a reference to the broadcast intent to be removed.

The Broadcast Intent Example

The remainder of this chapter will function through the creation of an example of broadcast intents in action. In the first instance, a simple application will be produced for the purpose of issuing a custom broadcast intent. A corresponding broadcast receiver will then be created that will display a message on the display of the device when the broadcast is detected. At the end, the broadcast receiver will be modified to detect the system notification of exterior power that is being disconnected from the device.

Creating the Example Application

The first step in this work out is to create an application, the rationale of which is to send a custom broadcast intent.

Within the Eclipse environment, create a new Android Application scheme. Name the scheme SendBroadcast, with the suitable package name and SDK selections.

Ask for the creation of a blank activity and the use of the default launcher icons. On the New Blank Activity screen of the New Android Application wizard, set the Activity Name to SendBroadcastActivity and the Layout and Fragment to activity_send_broadcast and fragment_send_broadcast correspondingly.

Click Finish to create the new scheme.

Once the new project has been created, find and load the fragment_send_broadcast.xml layout file and either in the Graphical Layout tool or XML editor replace the TextView object with a Button view. Move the button to the center of the display and alter the text so that it reads “Send Broadcast”. Right-click on the button in the Graphical Layout tool and select the Other Properties -> All by Name -> onClick… menu option and enter the technique name broadcastIntent into the resulting dialog.

 Creating and Sending the Broadcast Intent

Having created the framework for the SendBroadcast application,the next thing is to to implement the code to send the broadcast intent. This involves implementing the broadcastIntent() technique mentioned earlier as the onClick target of the Button view in the user interface. Find and double click on the SendBroadcastActivity.java file and modify it to add the code to create and send the broadcast intent. As soon as it is modified, the source code for this class ought to read as follows:

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.content.Intent;

public class SendBroadcastActivity extends Activity {
      public void broadcastIntent(View view) {
          Intent intent = new Intent();
          intent.setAction("com.example.SendBroadcast");
          intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
          sendBroadcast(intent);
      }
}

Observe that, in this instance, the action string for the intent is com.example.SendBroadcast. When the broadcast receiver class is created in later it is necessary that the intent filter declaration match this action string.

This concludes the creation of the application to send the broadcast intent. The only thing that is remaining is to build a matching broadcast receiver.

Creating the Broadcast Receiver

In order to create the broadcast receiver, a new class requires to be created which subclasses the BroadcastReceiver superclass. Create a new project named BroadcastReceiver with a suitable package name, this time switching off the Create Activity option in the Configure Project screen.

Add a fresh class to the scheme by right-clicking on the BroadcastReceiver scheme name in the Package Explorer panel and selecting the New -> Class menu option. In the Package: field, name the package com.example.broadcastreceiver. Name the class MyReceiver and enter or select android.content.BroadcastReceiver as the superclass and click on Finish to create the new class.

Go to the new MyReceiver.java file and double click on it to load it into an editing panel where it ought to read as follows:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context arg0, Intent arg1) {
   // TODO Auto-generated method stub
  }
}

As can be seen in the code, Eclipse has previously auto-generated for us the onReceive() method which will be called when a matching broadcast intent is noticed. This requires to be modified to show a message to the user with the use of the Toast class. The argument names (arg0 and arg1) ought to as well be altered to names that are more consequential:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub
     Toast.makeText(context, "Broadcast Intent Detected.",
     Toast.LENGTH_LONG).show();
   }
}

The code for the broadcast receiver is at this point complete. In order for the receiver to listen for the broadcast intents, nevertheless, an alteration to the manifest file is needed.

Configuring a Broadcast Receiver in the Manifest File

In common with other Android projects, BroadcastReceiver has connected with it a manifest file referred AndroidManifest.xml. At this stage, this file almost certainly bears the following XML structure:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    </application>
</manifest>

This file now requires to be modified to make public the presence of the broadcast receiver and to add an intent filter to indicate the broadcast intents in which the receiver is concerned. This involves adding a <receiver> section bearing an <intent-filter> element configured for the custom action string:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="MyReceiver" >
            <intent-filter>
                <action android:name="com.example.SendBroadcast" >
                </action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

With the manifest file concluded, the broadcast example is all set to be tested.

Listening for System Broadcasts

The final stage of this example is to modify the intent filter for BroadcastReceiver to listen also for the system intent that is broadcast when external power is disengaged from the device. The action that the receiver requires to be listening for in this context is android.intent.action.ACTION_POWER_DISCONNECTED. The modified manifest file ought to, as a result, at this point read as follows:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="MyReceiver" >
            <intent-filter>
                <action android:name="com.example.SendBroadcast" >
                </action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" >         
                </action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

Since the onReceive() method is at present going to be listening for two types of broadcast intent, it is advisable adjusting the code so that the action string of the current intent is as displayed in the toast message. This string can be gotten through a call to the getAction() technique of the intent object passed as an argument to the onReceive() technique:

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String message = "Broadcast intent detected " + intent.getAction();
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}

Test the receiver by re-installing the customized BroadcastReceiver package. Touching the button in the SendBroadcast application ought to at this point result in a new message bearing the custom action string:

Broadcast intent detected com.example.SendBroadcast

A t this point remove the USB connector that is at present supplying power to the device, at which point the receiver ought to report the following in the toast message:

Broadcast intent detected android.intent.action.ACTION_POWER_DISCONNECTED

OTHER INTERESTING POSTS:

Add a Comment