Android Broadcast Receivers Lecture Outcome At the end
Android Broadcast Receivers
Lecture Outcome At the end of this lecture you will able to; • Understand broadcast Receiver. Prof. Sainath Patil, VCET Vasai Rd. 2
Broadcasts overview • Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publishsubscribe design pattern. • These broadcasts are sent when an event of interest occurs. • For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Prof. Sainath Patil, VCET Vasai Rd. 3
Broadcasts overview • Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in (for example, some new data has been downloaded). • Apps can register to receive specific broadcasts. • When a broadcast is sent, the system automatically routes broadcasts to apps that have subscribed to receive that particular type of broadcast. Prof. Sainath Patil, VCET Vasai Rd. 4
Broadcasts overview • Broadcasts can be used as a messaging system across apps and outside of the normal user flow. • However, you must be careful not to misuse the opportunity to respond to broadcasts and run jobs in the background that can contribute to a slow system performance Prof. Sainath Patil, VCET Vasai Rd. 5
About system broadcasts • The system automatically sends broadcasts when various system events occur, such as when the system switches in and out of airplane mode. • System broadcasts are sent to all apps that are subscribed to receive the event. Prof. Sainath Patil, VCET Vasai Rd. 6
• The broadcast message itself is wrapped in an Intent object whose action string identifies the event that occurred • For example android. intent. action. AIRPLANE_MODE • The intent may also include additional information bundled into its extra field. • For example, the airplane mode intent includes a boolean extra that indicates whether or not Airplane Mode is on. Prof. Sainath Patil, VCET Vasai Rd. 7
Receiving broadcasts • Apps can receive broadcasts in two ways: through Ø Manifest-declared receivers and Ø Context-registered receivers Prof. Sainath Patil, VCET Vasai Rd. 8
Manifest-declared receivers • If you declare a broadcast receiver in your manifest, the system launches your app (if the app is not already running) when the broadcast is sent. Prof. Sainath Patil, VCET Vasai Rd. 9
Manifest-declared receivers • If you declare a broadcast receiver in your manifest, the system launches your app (if the app is not already running) when the broadcast is sent. Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. Prof. Sainath Patil, VCET Vasai Rd. 10
Manifest-declared receivers • To declare a broadcast receiver in the manifest, perform the following steps: i. Creating the Broadcast Receiver. ii. Registering Broadcast Receiver • There is one additional steps in case you are going to implement your custom intents then you will have to create and broadcast those intents. Prof. Sainath Patil, VCET Vasai Rd. 11
Creating the Broadcast Receiver • A broadcast receiver is implemented as a subclass of Broadcast. Receiver class and overriding the on. Receive() method where each message is received as a Intent object parameter. Prof. Sainath Patil, VCET Vasai Rd. 12
Creating the Broadcast Receiver A broadcast receiver is implemented as a subclass of Broadcast. Receiver class and overriding the on. Receive() method where each message is received as a Intent object parameter. public class My. Receiver extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { Toast. make. Text(context, "Intent Detected. ", Toast. LENGTH_LONG). show(); } } Prof. Sainath Patil, VCET Vasai Rd. 13
Registering Broadcast Receiver • An application listens for specific broadcast intents by registering a broadcast receiver in Android. Manifest. xml file. • Consider we are going to register My. Receiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process. Prof. Sainath Patil, VCET Vasai Rd. 14
Registering Broadcast Receiver • An application listens for specific broadcast intents by registering a broadcast receiver in Android. Manifest. xml file. Register for Intent to observe • Consider we are going to register My. Receiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process. Android Broadcast System Receiver Gets notification when Intent occurs Prof. Sainath Patil, VCET Vasai Rd. 15
Registering Broadcast Receiver • An application listens for specific broadcast intents by registering a broadcast receiver in Android. Manifest. xml file. • Consider we are going to register My. Receiver for system <application generated event ACTION_BOOT_COMPLETED which is android: icon="@drawable/ic_launcher" fired by android: label="@string/app_name" the system once the Android system has completed > the bootandroid: theme="@style/App. Theme" process. <receiver android: name="My. Receiver"> <intent-filter> <action android: name = "android. intent. action. BOOT_COMPLETED"> </action> </intent-filter> </receiver> </application> Prof. Sainath Patil, VCET Vasai Rd. 16
Registering Broadcast Receiver • An application listens for specific broadcast intents by registering a broadcast receiver in Android. Manifest. xml file. • Consider we are going to register My. Receiver for system <application generated event ACTION_BOOT_COMPLETED which is android: icon="@drawable/ic_launcher" fired by android: label="@string/app_name" the system once the Android system has completed > the. Now bootandroid: theme="@style/App. Theme" process. your Android device gets booted, whenever it will be <receiver android: name="My. Receiver"> intercepted by Broadcast. Receiver My. Receiver and <intent-filter> implemented logic inside on. Receive() will be executed. <action android: name = "android. intent. action. BOOT_COMPLETED"> </action> </intent-filter> </receiver> </application> Prof. Sainath Patil, VCET Vasai Rd. 17
System Generated Events • There are several system generated events defined as final static fields in the Intent class. A few important system events. 1. android. intent. action. BATTERY_CHANGED Sticky broadcast containing the charging state, level, and other information about the battery. 2. android. intent. action. BATTERY_LOW Indicates low battery condition on the device. Prof. Sainath Patil, VCET Vasai Rd. 18
System Generated Events 3. android. intent. action. BATTERY_OKAY Indicates the battery is now okay after being low 4. android. intent. action. BOOT_COMPLETED This is broadcast once, after the system has finished booting. 5. android. intent. action. BUG_REPORT Show activity for reporting a bug. Prof. Sainath Patil, VCET Vasai Rd. 19
System Generated Events 6. android. intent. action. CALL Perform a call to someone specified by the data. 7. android. intent. action. CALL_BUTTON The user pressed the "call" button to go to the dialer or other appropriate UI for placing a call. Prof. Sainath Patil, VCET Vasai Rd. 20
System Generated Events 8. android. intent. action. DATE_CHANGED The date has changed. 9. android. intent. action. REBOOT Have the device reboot. Prof. Sainath Patil, VCET Vasai Rd. 21
Context-registered receivers • Context-registered receivers receive broadcasts as long as their registering context is valid. • For an example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. • If you register with the Application context, you receive broadcasts as long as the app is running. Prof. Sainath Patil, VCET Vasai Rd. 22
Context-registered receivers To register a receiver with a context, perform the following steps: 1. Create an instance of Broadcast. Receiver br = new My. Broadcast. Receiver(); Prof. Sainath Patil, VCET Vasai Rd. 23
Context-registered receivers 2. Create an Intent. Filter and register the receiver by calling register. Receiver(Broadcast. Receive, Intent. Filte): Intent. Filter filter = new Intent. Filter(Connectivity. Manager. CONNECTIVITY_ACTION); filter. add. Action(Intent. ACTION_AIRPLANE_MODE_CHANGED); this. register. Receiver(br, filter); Prof. Sainath Patil, VCET Vasai Rd. 24
Context-registered receivers 3. To stop receiving broadcasts, call unregister. Receiver(android. content. Broadcast. Receiver) Be sure to unregister the receiver when you no longer need it or the context is no longer valid. Prof. Sainath Patil, VCET Vasai Rd. 25
• Be careful when you register and unregister the receiver, • for example, if you register a receiver in 0 n. Create(Bundle) using the activity's context, you should unregister it in on. Destroy() to prevent leaking the receiver out of the activity context. • If you register a receiver in on. Resume(), you should unregister it in on. Pause() to prevent registering it multiple X Do not unregister in on. Save. Instance. State(Bundle), because this isn't called if the user moves back in the history stack. Prof. Sainath Patil, VCET Vasai Rd. 26
Sending Broadcasts Android provides three ways for apps to send broadcast: 1. The send. Broadcast(Intent) method sends broadcasts to all receivers in an undefined order. • This is called a Normal Broadcast. • This is more efficient, but means that receivers cannot read results from other receivers, propagate data received from the broadcast, or abort the broadcast. Prof. Sainath Patil, VCET Vasai Rd. 27
Sending Broadcasts 2. The send. Ordered. Broadcast(Intent, String) method sends broadcasts to one receiver at a time. • As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. • The order receivers run in can be controlled with the android: priority attribute of the matching intent-filter; • Receivers with the same priority will be run in an arbitrary order. Prof. Sainath Patil, VCET Vasai Rd. 28
Sending Broadcasts 3. The Local. Broadcast. Manager. send. Broadcast() method sends broadcasts to receivers that are in the same app as the sender. • If you don't need to send broadcasts across apps, use local broadcasts. • The implementation is much more efficient (no interprocess communication needed) and you don't need to worry about any security issues related to other apps being able to receive or send your broadcasts. Prof. Sainath Patil, VCET Vasai Rd. 29
Broadcasting Custom Intents • If you want your application itself should generate and send custom intents then you will have to create and send those intents by using the send. Broadcast() method inside your activity class. • If you use the send. Sticky. Broadcast(Intent) method, the Intent is sticky, meaning the Intent you are sending stays around after the broadcast is complete. Prof. Sainath Patil, VCET Vasai Rd. 30
Broadcasting Custom Intents • If you want your application itself should generate and send custom intents then you will have to create and send those intents by using the send. Broadcast() method inside your activity class. broadcast. Intent(View view) { • Ifpublic you usevoid the send. Sticky. Broadcast(Intent) method, the Intent intent = new Intent(); is sticky, intent. set. Action("CUSTOM_INTENT"); meaning the Intent you are sending stays around after the broadcast is complete. send. Broadcast(intent); } Prof. Sainath Patil, VCET Vasai Rd. 31
Broadcasting Custom Intents • <application This intent CUSTOM_INTENT can also be registered in similar android: icon="@drawable/ic_launcher" way as we have registered system generated intent. android: label="@string/app_name" android: theme="@style/App. Theme" > <receiver android: name="My. Receiver"> <intent-filter> <action android: name="CUSTOM_INTENT"> </action> </intent-filter> </receiver> </application> Prof. Sainath Patil, VCET Vasai Rd. 32
Thank You Prof. Sainath Patil, VCET Vasai Rd. 33
- Slides: 33