Skip to main content

Broadcast receivers in Android - ABNHive

It responds to system-wide broadcast announcements. It used to display a persistent icon in the status bar of the device, vibrate, play a sound, flashlights, etc. to indicate to the user that something significant has happened in the background.


System Generated Events

The following table lists a few important system events.
  • android.intent.action.BATTERY_CHANGED - Sticky broadcast containing the charging state, level, and other information about the battery.
  • android.intent.action.BATTERY_LOW - Indicates low battery condition on the device.
  • android.intent.action.BATTERY_OKAY - Indicates the battery is now okay after being low.
  • android.intent.action.DATE_CHANGED - The date has changed.
  • android.intent.action.REBOOT - Have the device reboot.
  • android.intent.action.BOOT_COMPLETED - This is broadcast once after the system has finished booting.
  • android.intent.action.BUG_REPORT - Show activity for reporting a bug.
  • android.intent.action.CALL - Perform a call to someone specified by the data.
  • android.intent.action.CALL_BUTTON - The user pressed the "call" button to go to the dialer or other appropriate UI for placing a call.

Broadcast Receiver Example

In this sample application, a notification is generated when you change the system time. The notification when clicked leads the user to the Contacts. This is how the application works:

public class MyBroadcastReceiver extends BroadcastReceiver {
 
    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;
 
    @Override
    public void onReceive(Context context, Intent intent) {
 
        mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
 
        Notification notifyDetails = new Notification(R.drawable.android,
                "Time Reset!", System.currentTimeMillis());
 
        PendingIntent myIntent = PendingIntent.getActivity(context, 0,
                new Intent(Intent.ACTION_VIEW, People.CONTENT_URI), 0);
 
        notifyDetails.setLatestEventInfo(context, "Time has been Reset",
                "Click on me to view Contacts", myIntent);
 
        notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
        notifyDetails.flags |= Notification.DEFAULT_SOUND;
 
        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
 
    }

- Aswin Bhim Nath

Comments