63.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 onCreate() 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 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 intent is also displayed in the toast...