Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.

Similar presentations


Presentation on theme: "Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system."— Presentation transcript:

1 Cosc 5/4730 Broadcast Receiver

2 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.

3 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.

4 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.

5 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.

6 Declaration. import android.content.Context; import android.content.Intent; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //now do something with the intent. }

7 Registering a receiver Statically in the AndroidManifest.xml … This is the intent you want the receiver to receive

8 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.

9 Dynamic Local broadcast Example: Where mReceiver is a variable of type BroadcastReceiver(). @Override public void onResume() { super.onResume(); // Register mReceiver to receive messages. LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("CUSTOM_EVENT")); } @Override 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.

10 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.

11 Dynamic system Wide broadcast Where mReceiver is a variable of type BroadcastReceiver(). @Override public void onResume() { super.onResume(); // Register mReceiver to receive messages. getBaseContext().registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW)); } @Override protected void onPause() { //or onDestory() getBaseContext()unregisterReceiver(mReceiver); super.onPause(); }

12 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.

13 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.

14 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/monitorin g-device-state/battery-monitoring.html http://developer.android.com/training/monitorin g-device-state/battery-monitoring.html

15 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.

16 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.

17 References http://www.vogella.com/tutorials/AndroidBro adcastReceiver/article.html http://www.vogella.com/tutorials/AndroidBro adcastReceiver/article.html http://developer.android.com/reference/andr oid/content/BroadcastReceiver.html http://developer.android.com/reference/andr oid/content/BroadcastReceiver.html http://www.tutorialspoint.com/android/andro id_broadcast_receivers.htm http://www.tutorialspoint.com/android/andro id_broadcast_receivers.htm

18 Q A &


Download ppt "Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system."

Similar presentations


Ads by Google