Can you register multiple system events with one broadcast receiver in
android
I can't find this in the online documentation, but this is what I would
like to do:
In the AndroidManifest.xml add the following receiver:
<receiver android:name="my.package.SystemBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.intent.action.DEVICE_STORAGE_LOW" />
<action android:name="android.intent.action.LOCALE_CHANGED" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED" />
<action android:name="android.intent.action.AIRPLANE_MODE" />
<action
android:name="android.intent.action.CONFIGURATION_CHANGED" />
</intent-filter>
</receiver>
In the SystemBroadcastReceiver.java add the following code:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_LOW)) {
// stop the app to save battery
}
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_OKAY)) {
// start the app again
}
// if (...) { // next action, etc}
}
I would like to add all system events the app uses in this one receiver.
Or is this bad practice? Also use this receiver to start a Service like
BluetoothService, WifiService, NFCService, etc.
I have seen many tutorials that only mention one system event, not more
than one. Is that for a specific reason?
No comments:
Post a Comment