Broadcasting Events with Intents

Broadcasting Intents is simple. Within your application, construct the Intent you want to broadcast and use the sendBroadcast method to send it.

Set the action, data, and category of your Intent in a way that lets Broadcast Receivers accurately determine their interest. In this scenario the Intent action string is used to identify the event being broadcast, so it should be a unique string that identifies the event. By convention, action strings are constructed with the same form as Java package names:

public static final String NEW_LIFEFORM_DETECTED = "com.paad.action.NEW_LIFEFORM";

If you wish to include data within the Intent you can specify a URI using the Intent'data property. You can also include extras to add additional primitive values. Considered in terms of an event-driven paradigm, the extras equate to optional parameters passed into an event handler.

Listing 5-17 shows the basic creation of a broadcast Intent using the action defined previously, with additional event information stored as extras.

^^ LISTING 5-17: Broadcasting an Intent Available for download on Intent intent = new Intent(NEW_LIFEFORM_DETECTED); intent.putExtra("lifeformName", lifeformType);

intent.putExtra("longitude", currentLongitude); intent.putExtra("latitude", currentLatitude); sendBroadcast(intent);

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


  • Tauno
    What is intents to broadcast events in android?
    1 year ago

Post a comment