62.11 Listening for System Broadcasts
The final stage of this example is to modify the intent filter for the broadcast receiver to listen also for the system intent that is broadcast when external power is disconnected from the device. That action is android.intent.action.ACTION_POWER_DISCONNECTED. Modify the configureReceiver() method in the MainActivity.kt file to add this additional filter:
private fun configureReceiver() {
val filter = IntentFilter()
filter.addAction("com.ebookfrenzy.sendbroadcast")
filter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED")
receiver = MyReceiver()
registerReceiver(receiver, filter)
}
Since the onReceive() method in the MyReceiver.kt file is now going to be listening for two types of broadcast intent, it is worthwhile to modify the code so that the action string of the current...