‫Hello, my friends, and welcome back.
‫In this video, we'll learn about the broadcast receivers in Android.
‫A broadcast receiver in Android is a component that listens for system wide broadcast events or intents
‫and allows your application to respond to those events.
‫It's a fundamental mechanism for inter component communication and for reacting to various system or
‫application events.
‫Broadcast receivers can be registered statically in the manifest file or dynamically at runtime.
‫When any of those events occur.
‫It brings the application into action by either creating a status bar notification or performing a task.
‫Unlike activities, broadcast receiver doesn't contain any user interface.
‫Let's jump into Android studio and create an application that uses the broadcast receiver Open Android
‫Studio, Create a new project, empty Views activity and name it as broadcast like Java Click.
‫Finish this application will listen for changes in the airplane mode and displays a toast message when
‫the mode is tackled by using the broadcast receiver.
‫For that, I'll create a new Java class.
‫I name it as airplane mode receiver enter and I'll extend it from the broadcast receiver class.
‫The broadcast receiver is an abstract class with all receive method being abstract.
‫It's found in the Android package.
‫So if we click on control and Left Mouse, you can see this is the broadcast receiver Abstract class
‫Alt+ Enter to implement the methods.
‫We have only one method called on receive and the on receive method is a method that it is called when
‫the broadcast receiver receives a broadcast.
‫And if we look at the method signature, we have two parameters, one of type context and the other
‫of type intent.
‫The context.
‫This is the context of the application or component that registered the receiver.
‫It provides access to application specific resources and services.
‫The intent.
‫The second parameter, this is the broadcast intent that triggered the receiver.
‫It contains information about the broadcast event, such as the action and any extra data.
‫So on the receive method is called when there is broadcast receiver receives a broadcast.
‫I'll check here if intent dot.
‫This is the intent.
‫I'm checking the intent.
‫Dot get action method doesn't equal to null and intent.
‫Dot get action dot equals.
‫Just follow me.
‫I lecture everything later.
‫Intent dot action and here I'll use airplane mode change get action method is a method that helps us
‫to get the action.
‫The string that defines the type of event or message you are broadcasting.
‫It's important to choose a unique action string that specifies the purpose of your broadcast.
‫Other components that you want to receive This broadcast will also specify the same action string when
‫registering their broadcast receiver.
‫In this case I'm checking it if it is not null and the action is equal to the airplane mode change here.
‫I'm checking if the broadcast action is related to airplane mode change here.
‫I'll open brackets check for plane mode on.
‫I'm getting it from the intent dot get boolean extra and the state and I'll set it to false.
‫So I'll consider that the airplane mode is start by false.
‫I am checking for the message string message msg equal to is airplane mode.
‫On.
‫If it is true, then display airplane mode is on.
‫If it is not then display airplane mode is off.
‫This is a shortcut for if and else.
‫Okay, so this line retrieves the boolean extra named state from the intent.
‫This extra indicates whether the airplane mode is currently on or off.
‫If it is on, then we need to display the airplane mode is on message.
‫If it is false, then display airplane mode is off for that toast message.
‫I'll create new text message here and I'll display the message variable.
‫The third step is to register the receiver, the broadcast receiver.
‫In the manifest, you can see this is our application tag.
‫We have an activity which is a component of this.
‫Application.
‫So you can see this is the application.
‫We have a component called activity.
‫And here I need to create another component, but it is of type receiver which refers to broadcast receiver
‫name airplane mode receiver.
‫I am telling that this class will act as a broadcast receiver and I'm registering it in the manifest
‫here.
‫I need to set the intent filter or let me close the receiver at first.