Detailed explanation of example of Android intent sending broadcast message

Android intent sends broadcast messages

Another purpose of intent is to send broadcast messages. Both applications and Android systems can use intent to send broadcast messages. The content of broadcast messages can be data information closely related to applications or Android system information, such as changes in network connection, battery power, received SMS or system settings. If the application registers broadcastreceiver, it can receive the specified broadcast information.

Sending broadcast messages using intent is very simple. Just create an intent and call the sendbroadcast() function to broadcast the information carried by intent. However, it should be noted that when constructing intent, a globally unique string must be defined to identify the action to be performed, usually using the name of the application package. If you want to pass additional data in intent, you can use the putextra () method of intent. The following code constructs the Intent for broadcast messages, adds additional data, and then calls sendBroadcast () to send the broadcast message:

Broadcastreceiver is used to listen to broadcast messages. You can register a broadcastreceiver in the androidmanifest.xml file or code, and use the intent filter to specify the broadcast messages to be processed. To create a broadcastreceiver, you need to inherit the broadcastreceiver class and overload the onReceive () method. The example code is as follows:

When the Android system receives a broadcast message matching the registered broadcastreceiver, the Android system will automatically call the broadcastreceiver to receive the broadcast message. After the broadcastreceiver receives the matched broadcast message, the onreceiver () method will be called, but the onReceive () method must be executed within 5 seconds, otherwise the Android system will think that the component loses response and prompt the user to forcibly close the component.

Here is a simple example

Key code for sending broadcast message

Register the broadcastreceiver in the androidmanifest.xml file

A < receiver > node is created in the androidmanifest.xml file, which declares that the action of the intent filter is edu.hrbeu.broadcastreceiverdemo, which is consistent with the action of intent in sending broadcast messages, indicating that the broadcastreceiver can accept broadcast messages with the action of edu.hrbeu.broadcastreceiverdemo.

A custom broadcastreceiver is created in mybroadcastreceiver.java, and its core code is as follows:

The first line of the code first inherits the broadcastreceiver class, and overloads the onrecover () function in line 3. After receiving the broadcast message defined in the androidmanifest.xml file, the program will automatically call onrecover() function for message processing.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>