‫Welcome back.
‫In this video we're going to learn about the broadcast receivers in Android.
‫Here I created a new project called the Broadcast App that contains in its layout only one text view
‫acting as a title.
‫Now I want from you to focus with me because I'm going to introduce a very new topic, which is the
‫broadcast receiver.
‫Here inside the package, I need to create a new Kotlin class and I'll name it as Airplane Mode Receiver.
‫Click okay and to tell Android Studio that this class will act as a broadcast receiver.
‫I need to extend it from the broadcast receiver class, change the constructor invocation, and then
‫Alt Plus enter to implement the members and the member is on receive method.
‫Click okay and here we go.
‫We overrided this method.
‫But wait, what is broadcast receiver?
‫A broadcast receiver is an Android component that allows the registration and handling of system wide
‫broadcast messages.
‫It enables communication between different parts of an Android application or between different applications.
‫A broadcast receiver listens for broadcast messages announced by Android system or other applications.
‫These broadcasts can be system events, for example screen rotation changes, battery low events, or
‫custom broadcasts sent by applications.
‫In this example and in this application, we're going to we're going to listen for airplane mode changes.
‫So we're going to display a toast message telling the user the state of the airplane mode.
‫So whenever the user changes the airplane mode from off to on, we need to receive a toast message by
‫the help of this airplane mode receiver and display that the airplane is switched to on the on receive
‫method.
‫This method is called when the broadcast receiver receives a broadcast inside it.
‫I'll add my logic.
‫So here if intent I'm checking for this intent dot get action.
‫Action doesn't equal to null.
‫And I need to add this question mark.
‫And I'm checking if intent dot get action or action dot equals to intent dot action airplane mode changed
‫here I'm checking and listening for the airplane mode change.
‫Let's continue.
‫If there is a change, go and create here.
‫A boolean is airplane mode on which is a boolean equals to intent dot.
‫Get boolean extra.
‫Here I'm getting the state and putting it as false.
‫The default value.
‫As mentioned before, the on receive method has intent parameter and this intent parameter.
‫We are checking for it if it is not null and if this intent contains an action.
‫Talking about the airplane mode change, then I'm checking the intent extra to get the state key and
‫the value of it.
‫Now I need to check if it is on.
‫I need to display a toast message telling us that it is on.
‫If it is not, then display another toast message telling that the airplane mode is off here.
‫If is airplane mode on checking if it is true is true, then display a toast message.
‫And change this to the context that it is passed in the on receive parameter.
‫Else if it is not on, it is off.
‫I need to display a toast message telling that the airplane is off.
‫Okay, we finished the airplane mode receiver.
‫Now let's move to the main activity.
‫To register the broadcast receiver, you can register a broadcast receiver either statically in the
‫manifest file or dynamically in the code.
‫Static registration in the manifest is suitable for broadcasts that are intended to be received even
‫when the app is not running.
‫Dynamic registration allows for more flexibility, such as registering and unregistering the receiver
‫at runtime.
‫Personally, I prefer to register dynamically the broadcast receivers.