Listening for broadcasts

As we displayed in the previous example, we can receive broadcasts in one of the two following ways:

  • Registering the broadcast receiver through Android Manifest
  • Registering the broadcast using the registerBroadcast() method in the contexts

Declaring through Manifest requires the following:

  • The <receiver> element with android:name and android:exported parameters.
  • The receiver must contain intent filters for the actions we subscribe. Take a look at the following example:
        <receiver android:name=".OurBootReceiver"          android:exported="true"> 
          <intent-filter> 
            <action android:name= "android.intent.action.BOOT_COMPLETED"/> ... <action android:name="..."/> <action android:name="..."/> <action android:name="..."/> </intent-filter> ...

Get Mastering Android Development with Kotlin now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.