Cosc 5/4730 Broadcast Receiver
Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system or application events. – All registered receivers for an event are notified by the Android runtime once this event happens.
Uses. You app registers which events it wants to receive – Your receiver will then receive an intent when the “event” happens. Boot_completed is popular to start a background service on a reboot. » Note the app has to be launched once before this works, but it can be many reboots ago. Text messages custom messages like in the notifications lecture.
System Broadcasts EventDescription Intent.ACTION_BOOT_COMPLETED Boot completed. Requires the android.permission.RECEIVE_BOOT_COM PLETED permission. Intent.ACTION_POWER_CONNECTEDPower got connected to the device. Intent.ACTION_POWER_DISCONNECTEDPower got disconnected to the device. Intent.ACTION_BATTERY_LOW Triggered on low battery. Typically used to reduce activities in your app which consume power. Intent.ACTION_BATTERY_OKAYBattery status good again. Popular list, but this is not the complete list.
The Basics You can register a receiver in the (statically) AndroidManifest.xml or using (dynamically) the context.registerReceiver() method When an intent is received, then the onReceive() method is called. – A receiver then does it’s work and finishes. In API11+, you can call goAsync() and then the receiver can stay alive until PendingResult.finish() is called.
Declaration. import android.content.Context; import android.content.Intent; public class MyReceiver extends BroadcastReceiver public void onReceive(Context context, Intent intent) { //now do something with the intent. }
Registering a receiver Statically in the AndroidManifest.xml … This is the intent you want the receiver to receive
Registering a receiver (2) Normally in onResume() and onPause() If you forget to unregister, then you will get a “leaked broadcast receiver error” on exit. You can register for “local” events or system wide. – Local means only your application is going to send intents to it and very likely this custom intents.
Dynamic Local broadcast Example: Where mReceiver is a variable of type public void onResume() { super.onResume(); // Register mReceiver to receive messages. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("CUSTOM_EVENT")); protected void onPause() { //or onDestory() // Unregister since the activity is not visible LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); super.onPause(); } A note, I could not get this to work in the examples, so I used system wide register.
Send a broadcast Easily to do. Intent i = new Intent("SOME_ACTION"); sendBroadcast(i); Remember, you can add more data/information to the intent as we have done in many places. NOTE: you can not send system broadcast, like boot_completed.
Dynamic system Wide broadcast Where mReceiver is a variable of type public void onResume() { super.onResume(); // Register mReceiver to receive messages. getBaseContext().registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW)); protected void onPause() { //or onDestory() getBaseContext()unregisterReceiver(mReceiver); super.onPause(); }
Now what? So now your application can respond to an event. – Like launch an activity – Start a service – Or just doing something quickly in the receiver. Remember an intent can contain data in the bundle – so an intent you create, send via a pendingIntent through say alarm or notification service. – Others system events maybe have information as well.
System Broadcasts. A common one is for a application to start a service when the devices finishes it boot. – IE Run on startup and continue to run. – These services are common for polling for notifications and sort of thing. Example: – SnapChat (and many apps), It running in the background as a service polling every so often their services for new “messages”. Setups a notification when there are new messages. Should be noted, this a drain on the battery.
Battery and Charging. You app may want to know when the battery state changes. – You can be notified when the battery state and charging state changes. Using a receiver and intents listed before. Note, for immediate info, see the following: – g-device-state/battery-monitoring.html g-device-state/battery-monitoring.html
Screen on/off There is a broadcast to any running applications for the Screen On or Off – Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON – Your activity or service must be running Or the system will ignore your application. – This is an odd one because when the screen turns “off”, your activity likely just received an onPause() call too.
Demo code BroadCastDemo1 – Simple implementation of a receiver with a static and dynamic registered intent-filter BroadCastDemo2 – Setup to receive intents about battery status and power status. – Also uses android:launchMode="singleTop" so that only one activity is launched, when it is the foreground app. BroadcastNoti – A reimplementation of the notification demo, but using only receivers for the broadcast. BroadcastBoot – Receives a broadcast on boot, that starts a service. Note, you need to start the main activity once, and to see it really work, “reboot” the emulator.
References adcastReceiver/article.html adcastReceiver/article.html oid/content/BroadcastReceiver.html oid/content/BroadcastReceiver.html id_broadcast_receivers.htm id_broadcast_receivers.htm
Q A &