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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.