Detailed explanation of broadcast receiver component in Android development and Application

Broadcastreceiver is one of the four components in Android. The following describes the usage of the broadcast receiver component.

The following is an overview of broadcastreceiver in Android doc:

① Broadcast receiver is a component that focuses on receiving broadcast notification information and making corresponding processing. Many broadcasts come from system code -- for example, notifying a time zone change, a low battery, taking a picture, or a user changing a language option. Applications can also broadcast -- for example, notifying other applications that some data has been downloaded and is available.

② An application can have any number of broadcast receivers to respond to all the notification information it is interested in. All receivers inherit from the broadcastreceiver base class.

③ The broadcast receiver does not have a user interface. However, they can start an activity to respond to the information they receive, or notify the user with notification manager. Notifications can attract users' attention in many ways - flashing backlights, shaking, playing sounds, and so on. Generally, a persistent icon is placed on the status bar so that users can open it and get messages.

There are two kinds of broadcast events in Android. One is the system broadcast event, such as action_ BOOT_ Completed (triggered after system startup), action_ TIME_ Changed (triggered when the system time changes), action_ BATTERY_ Low (triggered when the battery is low), etc. The other is our custom broadcast event.

Broadcast event flow

① Register broadcast events: there are two registration methods. One is static registration, that is, in Android manifest Defined in the XML file, the registered broadcast receiver must inherit broadcastreceiver; The other is dynamic registration, which uses context in the program Registerreceiver is registered. The registered broadcast receiver is equivalent to an anonymous class. Intentfilter is required for both methods.

② Send broadcast event: through context Sent by sendbroadcast, and the action used in registration is passed by intent.

③ Receive broadcast event: when the broadcast sent is monitored by the receiver, it will call its onReceive () method and pass the intent object containing the message to it. The execution time of the code in onReceive should not exceed 5S, otherwise Android will pop up the timeout dialog.

Next, I demonstrate the use of custom broadcast events and system broadcast events through code.

Step 1: register broadcast events in the OnStart method of mainactivity. The static registration method is in Android manifest XML file.

Step 2: clicking the corresponding button will trigger the corresponding method to send the broadcast message.

Step 3: receive broadcast messages. The following are two statically registered broadcast receivers.

Here is androidmanifest XML file:

Interface layout file main xml

The explanation is over, but I didn't know one thing myself. If I send broadcast in the program, it is a custom broadcast. If not, does the system send the corresponding action broadcast by itself? If you know, please tell me and thank you first.

Operation interface:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>