Sunday, June 19, 2016

Android BroadcastReceiver

Android applications need some mechanism to know when some event is occurred. For example some applications need to perform tasks when Android OS booting is completed. Android SDK provide BroadcastReceiver class for that purpose. This blog post content is about using BroadcastReceiver to get notification about connecting and disconnecting Android OS running device from external power source.
Name of this application is ConDisConInfo. So it has the main package with name com.blogspot.nipunswritings.ConDisConInfo. To create a BroadcastReceiver follow these steps.
  • Right Click on Main package
  • New
  • Other
  • Broadcast Receiver
  • Give name and click Finish
Once Finish is clicked a new java file can be seen in main package with the name PowerConnInfoReceiver.java. It has a PowerConnInfoReceiver class which extends from super class BroadcastReceiver. PowerConnInfoReceiver has below method which receive power connection/disconnection information which is sent by system.

@Overridepublic void onReceive(Context context, Intent intent) {
    
}

After  PowerConnInfoReceiver is created AndroidManifest.xml file has been updated with receiver element. That is registering a Broadcast Receiver. The receiver element in AndroidManifest.xml file is this.

<receiver 
     android:name=".PowerConnInfoReceiver" 
     android:enabled="true"
     android:exported="true">
</receiver>

Though that element is in the AndroidManifest.xml file PowerConnInfoReceiver will not receive any broadcast. A BroadcastReceiver to receive broadcasts it must be specified what broadcasts it must be received through the intent-filter element. Therefore receiver element is updated like below to receive broadcasts as mentioned above.

<receiver 
    android:name=".PowerConnInfoReceiver" 
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
    </intent-filter>
</receiver>

Notice the actions inside intent-filter. Without those actions this application doesn't work as expected. 

Method onReceive(Context, Intent) is updated to filter received information via Intent object and to display information to user as a Toast. So updated onReceive(Context, Intent) method is this.

@Overridepublic void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)){
        Toast.makeText(context, "Power Connected", Toast.LENGTH_LONG).show();
    } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
        Toast.makeText(context, "Power Disconnected", Toast.LENGTH_LONG).show();
    }
}

Run this app on a emulator. This application can be tested with battery section in extended controls of emulator. When charger connection is toggled between AC Charger and None corresponding Toast is displayed.



Reference:
  • https://developer.android.com/reference/android/content/BroadcastReceiver.html
  • https://developer.android.com/reference/android/content/Intent.html#ACTION_POWER_CONNECTED
  • https://developer.android.com/reference/android/content/Intent.html#ACTION_POWER_DISCONNECTED

No comments:

Post a Comment