What are BroadcastReceiver in Android systems, why and how to use them?
10 minute(s) read | Published on: May 04, 2021 Updated on: Dec 14, 2021 |
We need to be aware that we must apply some special settings so that we can finally be informed of those events and happenings. The settings must be applied so that the application is aware of the events and happenings in some operating systems and responds to them appropriately, which is possible by using BroadcastReceiver components.
Here are the things we will learn in this tutorial:
- What is BroadcastReceiver?
- How to implement BroadcastReceiver on Android - How to build a BroadcastReceiver - How to BroadcastReceiver
- How to register a BroadcastReceiver in the manifest using the static method
- How to record BroadcastReceiver inactivity using dynamic method
- What are the most important events of Android systems?
- How to publish different types of messages in Android systems
What is BroadcastReceiver?
To say what BroadcastReceiver is, we need to give you some examples. For example, suppose we want to create an application connected to the Internet and is very dependent on the Internet. In this case, if the user is using the application and the application is not connected to the Internet, it must be Informed the user so that the user is informed that it is not connected to the Internet. In other words, it can be said that not connecting to the Internet must be notified to the user to take the necessary actions.
Consider that the user wants to use an application in other examples and situations, which requires entering a verification code. Verification codes are sent via messages, and users must enter them in the application's text box after receiving it to enter the application and use it. In this case, we should be notified of events that occur in applications and operating systems to apply settings that automatically enter the verification code sent in the text box without entering the user.
To do this, we must be aware of the events in the system to finally make these settings, but we can also use BroadcastReceiver.
How to implement BroadcastReceiver on Android
1- We create a new project in Android Studio with the desired name BroadcastReceiver. It should be noted that the name of this project has been chosen arbitrarily in this BroadcastReceiver tutorial, and you can choose other names for this project.
2- The activity used in this project must be of the Empty Activity type.
3- We need to create a BroadcastReceiver.
4- It should be noted that to create BroadcastReceiver, we must create a new class that inherits from BroadcastReceiver.
5- Override this class's onReceive () method to receive the relevant messages. Note: Messages are received in intent format.
6- I create a Java class inside the project with the desired name MyReceiver.
7- The code that should be included in MyReceiver.java is as follows:
8- How to override the onReceive () method can be done manually and automatically.
9- Go to the body of the class and press alt + enter.
10- Implement the onReceive method.
11- The method has been added as follows. The code that should be included in the MyReceiver.java section is as follows:
12- We complete the class as follows. The code that should be included in the MyReceiver.java section is as follows:
Entering the above codes, we cause the system to announce the connection status message to the user. Note that the message indicates a connection status change and does not indicate a connection or no connection. This is why the user must check the connection or not connection.
In other words, the system sends a message to the user saying that the connection status has changed but does not say whether the system is connected to the Internet or not. Therefore, the user must check whether the Internet is connected or not.
13- To check the status of connection or non-connection to the Internet, I define a method with the desired name check network inside the class and boolean type.
By entering the above code, the Internet connection status is checked using the ConnectivityManager and getActiveNetworkInfo () methods, and the check result is displayed in nInfo. After checking the connection or not connecting to the Internet, the result will be displayed in nInfo. The method we used for this is boolean, and the result that is going to return to us can be true or false.
We put the code inside the try-catch to not crash and continue until the output and result are empty. To prevent the program from crashing when the result is null, we must put the code and commands inside the try-catch until the program is null, the program continues.
Then inside the onReceive () method, we perform the operation that gives us according to the check network result.
If the received result is true, a message with the content "Connection established" will appear, and if the result is false, the message "Connection not established" will appear.
14- After creating the BroadcastReceiver, we must register it in the operating system. Note: Unless you register and save the class as a Receiver, you will not be notified of events.
- How to record a BroadcastReceiver using the static method in the manifest
Open the project manifest and add the receiver tag to the application tag. As follows:
15- To access them, we must add the required license to the manifest.
16- The codes that should be in the AndroidManifest.xml section are as follows:
Note: There are many actions in the operating system, each used and used for some purpose.
For example, the action android. Intent. Action. BOOT_COMPLETED is used to notify the system that it is booting and if the system boots, it displays the appropriate message.
- How to record BroadcastReceiver using dynamic activity method
We registered the receiver in the manifest receiver in the previous method. In this method, we want to register it in the activity. In addition to the fact that it is possible to register a receiver in the manifest in a static way, it is also possible to register it in an activity in a dynamic way.
17- The codes that should be in the MainActivity.java section are as follows:
In the code above, we define a condition inside the onCreate () method to check if the program executes the commands listed below and runs on N-OS or higher. Therefore, the condition that we put in the code listed above checks if the version of the operating system it uses is N or higher, it executes the code and commands listed below:
18- In the code above, I create an instance of the receiver class.
19- Then, I register the desired action using the registered receiver () method. This method has two inputs: the receiver class, and the second is intent.
20- The unregister Receiver method is used to disable the Broadcast of the registered receiver.
21- The register receiver method is also used to register the BroadcastReceiver.
22- The code that should be in the MainActivity.java section is as follows:
23- I will implement the project.
24- Turn Wi-Fi on and off.
25- After running the project and activating and deactivating WIFI, we will see that the message "Connection is established" will be displayed when WIFI is turned on. If the WIFI connection is deactivated, "Connection is not established" will be displayed.
The onReceive () method will be executed when the action message is defined.
26- I also define a listener for the message receiving an event.
27- To do this, we must first define the manifest's license and action related to receiving SMS.
28- The codes that should be in the AndroidManifest.xml section are as follows:
29- Then, I add a Toast for the onReceive () method:
Note: In Android version 6 and above, the user must approve some licenses, one of which is access to SMS from the approved user.
30- I define a Button in the activity layout.
31- The codes that should be in the activity_main.xml section are as follows:
Note: Obtaining access permissions has nothing to do with the method of registering receivers, and you can obtain access permissions in the same way from any method you use to register.
32- Add Runtime Permission codes to the activity.
33- The code that should be in the MainActivity.java section is as follows:
34- We will run the project again.
35- Click the button to give RECEIVE_SMS permission to the system.
36- We send a message to the device.
37- We will see a message appear that contains the following:
Event Name Event Application
In the above sections, we got acquainted with the system's messages sent and published automatically. Still, it should be noted that we can create some messages ourselves in a custom way, and other programs will also receive these messages.
The following code can be called at the click of a button:
We created a custom action with a custom name by section (). The action defined by send broadcast () is released at the operating system level. If necessary, more information can be sent by intent, which can be done by putExtra () :
The action tag of the BroadcastReceiver class is defined as follows:
Note: In addition to the send broadcast () method, there is also the send sticky Broadcast () method, which differs in that the send sticky broadcast () method has high durability. Persistence means that the messages that are sent can be stored in memory.
How to publish a message with permission
We may want to apply the settings to send and publish a message, which also needs permission. In addition to publishing the message, we must also define a license for it. In this case, the message receives permission from the user, system, or device to be published and then published.
How to send custom messages within the app
In some cases, we may publish a message that only the program itself receives and sees, and other programs do not receive the message. This method also has advantages; Among other things, security increases, and other programs cannot access and listen to the published messages.
To do this, we can use the LocalBroadcastManager class, which Android has created for sending and publishing messages within the application.
In this educational article, you are familiar with listening and being informed about the events that occur in the Android system. You can be informed about the events of the Android system by following the step-by-step steps mentioned in this article. As mentioned, some events take place on Android systems, and to be informed about them, you must apply some settings to receive appropriate messages when events occur. Here are some examples of events and their messages.
About our Android and iOS app development services