Presentation is loading. Please wait.

Presentation is loading. Please wait.

Broadcast Receivers A Android Component where you can register for system or application events, receive and react to broadcast intent. Each broadcast.

Similar presentations


Presentation on theme: "Broadcast Receivers A Android Component where you can register for system or application events, receive and react to broadcast intent. Each broadcast."— Presentation transcript:

1 Broadcast Receivers A Android Component where you can register for system or application events, receive and react to broadcast intent. Each broadcast is delivered as a intent object It extends the class BroadcastReceiver You will be notified about the events after registering. It plays a role of “No UI gateway” to do some small work such as starting a service or activity based on the received notice/event Broadcast originates from the system as well as applications. Ex: Instance for broadcast originating from the system: ‘low battery notification’, SMS received Ex: Application level is, like when you download an mp3 file, Mp3 player gets notified about it, and gets added to player list. For this action to take place, mp3 player has to register for this event.

2 Two ways to register Android broadcastreceiver
One is static way in which the broadcast receiver is registered in an android application via AndroidManifest.xml file. Another way of registering the broadcast receiver is dynamic, which is done using Context.registerReceiver() method. Dynamically registered broadcast receivers can be unregistered using Context.unregisterReceiver() method.

3 <. xml version="1. 0" encoding="utf-8"
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=" package="com.example.broadcastreceiverdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" > <activity android:name="com.example.broadcastreceiverdemo.MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="BroadCastReceiver_Name" > <action android:name="Event_Name" > </action> </receiver> </application> </manifest>

4 onReceive() Create your Broadcast receiver and Implement onReceive() method of BroadcastReceiver abstract class Whenever the event occurs Android calls the onReceive() method on the registered broadcast receiver. For example, if you register for ACTION_POWER_CONNECTED event then whenever power got connected to the device, your broadcast receiver’s onReceive() method will be invoked. The onReceive() method takes two arguments. Ubli class myReceive extends BroadcastReceiver{ public void onReceive(Context context, Intent intent){ ToDo something }} Context is used to start services or activities and the intent is object with the action you used to register your receiver.

5 Register your Broadcast Receiver
The broadcast receiver can statically be registered via AndroidManifest.xml as said earlier. The element is used to specify the event the receiver should react to. So whenever this event ‘MyBroadcast’ occurs MyBroadcastReceiver’s onReceive() method will be invoked. As soon as the onReceive() method is finished, your BroadcastReceiver terminates.

6 Sample Application

7 REGISTER THE BROADCAST RECEIVER PROGRAMMATICALLY/DYNAMICALLY
BroadcastReceiver myReceiver=new MyBroadcastReceiver(); registerReceiver( this.myReceiver, new IntentFilter("MyBroadcast"));


Download ppt "Broadcast Receivers A Android Component where you can register for system or application events, receive and react to broadcast intent. Each broadcast."

Similar presentations


Ads by Google