System broadcasts and services System broadcast events Event
System broadcasts and services
System broadcast events. Event Description Intent. ACTION_BOOT_COMPLETED Boot completed. Requires the android. permission. RECE IVE_BOOT_COMPLETEDpermission. Intent. ACTION_POWER_CONNECTED Power got connected to the device. Intent. ACTION_POWER_DISCONNECT Power got disconnected to the device. ED 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.
Registration for the BOOT_COMPLETED <uses-permission android: name="android. permission. RECEIVE_BOOT_COM PLETED" /> <receiver android: name="My. Schedule. Receiver" > <intent-filter> <action android: name ="android. intent. action. BOOT_COMPLETED" /> </intent-filter> </receiver> <receiver android: name="My. Start. Service. Receiver" > </receiver>
The receiver would start the service import android. content. Broadcast. Receiver; import android. content. Context; import android. content. Intent; public class My. Receiver extends Broadcast. Receiver { @Override public void on. Receive(Context context, Intent intent) { // assumes Word. Service is a registered service Intent intent = new Intent(context, Word. Service. class); context. start. Service(intent); } }
Tip If your application is installed on the SD card, then it is not available after the android. intent. action. BOOT_COMPLETED event. In this case register it for the android. intent. action. ACTION_EXTERNAL_APPLI CATIONS_AVAILABLE event.
Exercise: Define receiver for phone changes Implement receiver Register your receiver in your Android. Manifest. xml <uses-permission android: name="android. permission. READ_PHONE _STATE" > </uses-permission>
<receiver android: name="My. Phone. Receiver" > <intent-filter> <action android: name = "android. intent. action. PHONE_STATE" > </action> </intent-filter> </receiver>
Implements on an Activity @Override public void on. Receive(Context context, Intent intent) { Bundle extras = intent. get. Extras(); if (extras != null) { String state = extras. get. String(Telephony. Manager. EXTRA_STATE); Log. w("MY_DEBUG_TAG", state); if (state. equals(Telephony. Manager. EXTRA_STATE_RINGING)) { String phone. Number = extras . get. String(Telephony. Manager. EXTRA_INCOMING_NUMBER); Log. w("MY_DEBUG_TAG", phone. Number); } } }
Validate implementations Install your application and simulate a phone call via the DDMS perspective in Eclipse. Validate that your receiver is called and logs a message to the Log. Cat view.
Exercise: System services and receiver Implement project Create broadcast receiver class public class My. Broadcast. Receiver extends Broadcast. Receiver { }
@Override public void on. Receive(Context context, Intent intent) { Toast. make. Text(context, "Don't panik but your time is up!!!!. “, Toast. LENGTH_LONG). show(); // Vibrate the mobile phone Vibrator vibrator = (Vibrator) context. get. System. Service(Context. VIBRATOR_SERVIC E); vibrator. vibrate(2000); }
Register this class as a broadcast receiver in Android. Manifest. xml <uses-permission android: name="android. permission. VIBRATE" > </uses-permission> <receiver android: name="My. Broadcast. Receiver" > </receiver>
Change the code of your Alarm. Activity class public void start. Alert(View view) { Edit. Text text = (Edit. Text) find. View. By. Id(R. id. time); int i = Integer. parse. Int(text. get. Text(). to. String()); Intent intent = new Intent(this, My. Broadcast. Receiver. class); Pending. Intent pending. Intent = Pending. Intent. get. Broadcast(this. get. Application. Context( ), 234324243, intent, 0); Alarm. Manager alarm. Manager = (Alarm. Manager) get. System. Service(ALARM_SERVICE); alarm. Manager. set(Alarm. Manager. RTC_WAKEUP, System. current. Time. Millis() + (i * 1000), pending. Intent); Toast. make. Text(this, "Alarm set in " + i + " seconds", Toast. LENGTH_LONG). show();
Validate implementation Run your application on the device. Set your time and start the alarm. After the defined number of seconds a Toast should be displayed. Keep in mind that the vibration alarm does not work on the Android emulator.
Defining custom events and receivers Registering broadcast receiver for custom events You can register a receiver for your custom actions. <receiver android: name="My. Receiver" > <intent-filter> <action android: name= "de. vogella. android. mybroadcast" /> </intent-filter> </receiver>
Sending broadcast intents The send. Broadcast() allows you to send intents to your registered receivers. Intent intent = new Intent(); intent. set. Action("de. vogella. android. mybroadcast"); send. Broadcast(intent); You cannot trigger system broadcasts events. The Android system will prevent this.
Tip The receivers are called asynchronous, i. e. , the send. Broadcast() method return immediately and does not wait until the receivers have executed.
. Local broadcast events with Local. Broadcast. Manager The Local. Broadcast. Manager class is used to register for and send broadcasts of Intents to local objects within your process. This is faster and more secure as your events don't leave your application.
@Override public void on. Resume() { super. on. Resume(); // Register m. Message. Receiver to receive messages. Local. Broadcast. Manager. get. Instance(this). register. R eceiver(m. Message. Receiver, new Intent. Filter("myevent")); }
Handler for received Intents for the "my-event" event private Broadcast. Receiver m. Message. Receiver = new Broadcast. Receiver() { @Override public void on. Receive(Context context, Intent intent) { // Extract data included in the Intent String message = intent. get. String. Extra("message"); Log. d("receiver", "Got message: " + message); } };
@Override protected void on. Pause() { // Unregister since the activity is not visible Local. Broadcast. Manager. get. Instance(this). unregiste r. Receiver(m. Message. Receiver); super. on. Pause(); }
// This method is assigned to button in the layout// via the on. Click property public void on. Click(View view) { send. Message(); } // Send an Intent with an action named "my-event". private void send. Message() { Intent intent = new Intent("my-event"); // add data intent. put. Extra("message", "data"); Local. Broadcast. Manager. get. Instance(this). send. Broadc ast(intent); }
Dynamic broadcast receiver registration Dynamically registered receiver Receiver can be registered via the Android manifest file. You can also register and unregister a receiver at runtime via the Context. register. Receiver() and Context. unregister. Receiver() m ethods.
Warning Do not forget to unregister a dynamically registered receiver by using Context. unregister. Receiver() method. If you forget this, the Android system reports a leaked broadcast receiver error. For instance, if you registered a receive in on. Resume() methods of your activity, you should unregister it in the on. Pause() method.
You can use the Package. Manager class to enable or disable receivers registered in your Android. Manifest. xml file. Component. Name receiver = new Component. Name(context, my. Receiver. class); Package. Manager pm = context. get. Package. Manager(); pm. set. Component. Enabled. Setting(receiver, Package. Manager. COMPONENT_ENABLED_STATE_ENABL ED, Package. Manager. DONT_KILL_APP);
Sticky (broadcast) intents An intent to trigger a receiver (broadcast intent) is not available anymore after it was sent and processed by the system. If you use the send. Sticky. Broadcast(Intent) method, the corresponding intent is sticky, meaning the intent you are sending stays around after the broadcast is complete. The Android system uses sticky broadcast for certain system information. For example, the battery status is send as sticky intent and can get received at any time. The following example demonstrates that.
// Register for the battery changed event Intent. Filter filter = new Intent. Filter(Intent. ACTION_BATTERY_CHANGED); // Intent is sticky so using null as receiver works fine // return value contains the status Intent battery. Status = this. register. Receiver(null, filter); // Are we charging / charged? int status = battery. Status. get. Int. Extra(Battery. Manager. EXTRA_ST ATUS, -1); boolean is. Charging = status == Battery. Manager. BATTERY_STATUS_CHARGING || status == Battery. Manager. BATTERY_STATUS_FULL;
boolean is. Full = status == Battery. Manager. BATTERY_STATUS_FULL; // How are we charging? int charge. Plug = battery. Status. get. Int. Extra(Battery. Manager. EXTRA_ PLUGGED, -1); boolean usb. Charge = charge. Plug == Battery. Manager. BATTERY_PLUGGED_USB; boolean ac. Charge = charge. Plug == Battery. Manager. BATTERY_PLUGGED_AC;
You can retrieve that data through the return value of register. Receiver(Broadcast. Receiver, Intent. Filter). This also works for a null Broadcast. Receiver. In all other ways, this behaves just as send. Broadcast(Intent). Sticky broadcast intents typically require special permissions.
- Slides: 29