How to listen for an intent using a Broadcast Receiver in an Activity

Oscar de la Hera Gomez
Three flowers that represent Kotlin, Android and Android Studio side by side. Beneath them sits the text “Receive Broadcasts in Activities.”

A step by step guide on listening for an intent, that may or may not have extras, using a Broadcast Receiver in an Activity in Kotlin and Android Studio.

This post is a part of a mini series on sending and receiving functional intents in an Android app. The rest of the tutorials are available at the end.

Please note that we have chosen to use the term "Notifications" to standardize naming conventions across iOS and Android. If you wish to exchange Notifications for Intents, we welcome you too.

This particular posts demonstrates how to listen for a language content update notification. This tutorial can be found in the main branch of our Open Source repository

Step One: Setup the Notifications Coordinator

A screenshot of Android Studio showing the NotificationCoordinator.kt file. Code available below.

For intents to be registered across an app it must be sent from a central manager, which we call a NotificationsCoordinator.

Step Two: Broadcast the Intent

A screenshot of Android Studio showing how we created the sendOnLanguageUpdateIntent  function in the NotificationCoordinator and sent it from the Language Coordinator.

Follow the tutorial below to learn how to structure a project to manage and broadcast intents.

Step Three: Declare the Broadcast Receiver

A screenshot of Android Studio showing you how to declare a Broadcast receiver in an activity. Code available below.

At the top of your MainActivity.kt declare your broadcast receiver using code similar to that below.

The example of the reciever below routes the intents to functions declared in the next step.

Step Four: Create the Notifications Extension

A screenshot of the Notification Extension of the MainActivity. Code available below.

Create a new file called MainActivity+Notifications.kt and paste in the code below.

This is where you will declare:

  • What Intents your receiver will listen for, whether they are internal or external.
  • All the functionality to respond to intents being received.

Please note that this functionality should be called from the receiver declared in previous step.

Step Five: Setup Notifications

A screenshot of Android Studio showing the setupNotifications function being called in the onCreate lifecycle in the MainActivty.kt file.

In the MainActivity.kt onCreate function, call setupNotifications() to make sure that the broadcast receiver is registered.

setupNotifications()

Step Five: Unregister the Receiver

A screenshot of Android Studio showing how to unregister a receiver in the onDestroy lifecycle function.

In the onDestroy lifecycle function of the MainActivity, unregister the broadcast receiver using the code below.

unregisterReceiver(broadcastReceiver)

Step Six: Test

A screenshot of Android Studio showing the logs that confirm that the notifications were sent and received.

Run the app in Android Studio and confirm that the notification is sent and received.

Looking to learn more about Intents in Android ?

Consult the tutorials linked below to learn more about sending and receiving Intents in Android and Kotlin.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details