Cosc 54730 Broadcast Receiver Broadcast receiver A broadcast
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. – Basically a listener, but for “broadcast” information. • Such as sms and other broadcast data. • Including our own broadcast intents. • Note, you don’t have access to most screen widgets in a broadcast receiver, except toast.
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 Popular list, but this is not the complete list. Event Description Intent. ACTION_BOOT_COMPLETED Boot completed. Requires the android. permission. RECEIVE_BOOT_COMPLETED permission. Intent. ACTION_POWER_CONNECTED Power got connected to the device. Intent. ACTION_POWER_DISCONNECTED Power 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_OKAY Battery status good again.
The Basics • You can register a receiver in the (statically) Android. Manifest. xml or using (dynamically) the context. register. Receiver() method • When an intent is received, then the on. Receive() method is called. – A receiver then does it’s work and finishes. • In API 11+, you can call go. Async() and then the receiver can stay alive until Pending. Result. finish() is called.
Declaration. public class My. Receiver extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { //now do something with the intent. } } • Intent Tells you what it is, via the get. Action() method.
Registering a receiver Statically in the Android. Manifest. xml <application …> <activity…> … </activity> <receiver android: name=“my. Receiver" > <intent-filter> <action android: name="android. intent. action. BATTERY_LOW" > </action> </intent-filter> </receiver> This is the intent you want the receiver to receive </application>
Registering a receiver (2) • Normally in on. Resume() and on. Pause() • 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 m. Receiver is a variable of type Broadcast. Receiver(). @Override public void on. Resume() { super. on. Resume(); // Register m. Receiver to receive messages. Local. Broadcast. Manager. get. Instance(this). register. Receiver(m. Receiver, new Intent. Filter("CUSTOM_EVENT")); } @Override protected void on. Pause() { //or on. Destory() // Unregister since the activity is not visible Local. Broadcast. Manager. get. Instance(this). unregister. Receiver(m. Receiver); super. on. Pause(); } Must use Local. Broadcast. Manager. get. Instance(get. Context()). send. Broadcast(i); in order to Receive if they are registered local.
Send a broadcast • Easy to do: Intent i = new Intent("SOME_ACTION"); • Global send. Broadcast(i); – get. Activity(). send. Broadcast(i); • Local must use – Local. Broadcast. Manager. get. Instance(get. Context()). send. Broadcast(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 m. Receiver is a variable of type Broadcast. Receiver(). @Override public void on. Resume() { super. on. Resume(); // Register m. Receiver to receive messages. get. Base. Context(). register. Receiver(m. Receiver, new Intent. Filter(Intent. ACTION_BATTERY_LOW)); } @Override protected void on. Pause() { //or on. Destory() get. Base. Context()unregister. Receiver(m. Receiver); super. on. Pause(); }
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 pending. Intent 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: – Snap. Chat (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: – http: //developer. android. com/training/monitoring-devicestate/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 on. Pause() call too.
API 26+ • Static register broadcast receivers (androidmanifest. xml) – Most implicit broads no longer work (almost many services too) – Only the following are except: https: //developer. android. com/guide/components/broadcast-exceptions. html • Note action_boot_completed is on the list, but not the ac or battery. • All dynamic register broadcast receivers work. • Implicit and explicit broadcasts. – Implicit is a system wide broadcast, which is what most of the previous slides show. • Most of these no longer work. – Explicit broadcasts are directed at an application only. • These all work.
Explicit broadcasts • We need the action and the package for explicit • So the code now looks like this (and works all API 9+) Intent i = new Intent(Main. Activity. ACTION 1); i. set. Package("edu. cs 4730. broadcastdemo 1"); //explicit get. Activity(). send. Broadcast(i);
Demo code • Broad. Cast. Demo 1 – Simplementation of a receiver with a static and dynamic registered intent-filter. The dynamic one is also a localbroadcast example. • Broad. Cast. Demo 2 – Setup to receive intents about battery status and power status. – Uses dynamic only registrations • Broadcast. Noti – A reimplementation of the notification demo, but using only receivers for the broadcast. • Broadcast. Boot – 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 • http: //www. vogella. com/tutorials/Android. Broadcast. Receiver/ article. html • http: //developer. android. com/reference/android/content/Bro adcast. Receiver. html • http: //www. tutorialspoint. com/android_broadcast_re ceivers. htm
Q&A
- Slides: 20