Transcript
Page 1: Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast

Using Intents to Broadcast Events Intents

Can be used to broadcast messages anonymously Between components via the sendBroadcast method

As a result Broadcast Receivers can be used

To listen for and respond to these Broadcast Intents

Android uses Broadcast Intents To broadcast system events

Such as changes in network connectivity and incoming calls

Page 2: Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast

Programmatically Registering a Broadcast Receiver For a receiver to receive broadcasts

It must be registered either in code or within the application manifest

To create a new Broadcast Receiver Extend the BroadcastReceiver class and

Override the onReceive event handler

To register the BroadcastReceiver object Use the registerReceiver method

To unregister the BroadcastReceiver object Use the unregisterReceiver method

Page 3: Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast

Registering a Broadcast Receiver in the manifest file In the case of applications including manifest Receivers

The applications don’t have to be running When the Intent is broadcast for those receivers to execute

To include a Broadcast Receiver in the manifest Add a receiver Tag within the application node

<receiver android:name=“.MyBroadcastReceiver”><intent-filter>

<action android:name=“MY_ACTION” /></intent-filter>

</receiver>

Page 4: Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast

Assigning Priorities to Broadcast Receivers When sending a broadcast using the sendBroadcast()

All the broadcast receivers that match the specified action Are called in random fashion

What if you want to assign a particular order to receivers? In this case, you need to assign a priority to broadcast receivers The setPriority() method

Takes priority values between 0 and 1000 The larger the number, the higher priority is

To set the priority of receivers in the manifest file:<intent-filter android:priority=“50”>

<action android:name=“MY_ACTION” /></intent-filter>

Page 5: Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast

Assigning Priorities to Broadcast Receivers (Cont’d) To send a broadcast

That is delivered to receivers with high priority first You should use sendOrderedBroadcast()

When a broadcast receiver of higher priority Receives the broadcast will be passed to next receiver

To handle the broadcast and Stop the broadcast from propagating to next receiver

Use the abortBroadcast()

Page 6: Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast

Auto-Launching Your App at Boot Time If you need to automatically start your app

Whenever the device starts up, you need a receiver That responds to Boot Completed broadcasts

To auto-launch an app during boot-up Add a new class to your package and derive it from

The BroadcastRecevier base class

In this way, when the device boots up It will fire your broadcast receiver calling onReceive method


Recommended