Broadcastreceiver usage examples of the four components of Android programming

This article describes the use of broadcastreceiver (broadcast receiver) of the four components of Android programming. Share with you for your reference, as follows:

Here we introduce how to create a broadcast, how to send an unordered broadcast and an orderly broadcast, and how to monitor text messages and outgoing calls (when we send text messages and make calls, the system will send a broadcast. We can intercept this broadcast to monitor text messages and outgoing calls).

Define broadcast recipients

1. Define a class to inherit broadcastreceiver and override onReceive method 2. After receiving a matching broadcast, onReceive method will be executed 3. Declare < receiver > in the manifest file, where < intent Filter > needs to be configured to specify the action and type of receiving broadcast 4. Broadcastreceiver can be declared in the code in addition to the manifest file, Register the receiver using the registerreceiver method

Send broadcast

Disordered broadcasting

1. Send by sendbroadcast method 2. Receive by all broadcast recipients in disorder and can not be interrupted 3. Set the receiver permission during broadcasting and receive only when the receiver has permission 4. The receiver's < receiver > can also set the sender permission to receive only the broadcast with permission application

Orderly broadcasting

1. Use sendorderedbroadcast method to send. 2. The receiver can define Android: priority in < intent Filter >, and the higher the number, the higher the priority. 3. It is received one by one by each broadcast receiver, and data can be interrupted or added in the middle

Monitor SMS reception

1. The Android system will send an orderly broadcast when receiving a short message. If we define a receiver to receive the broadcast, we can get the short message content or intercept the short message. 2. Define the broadcast receiver to receive the broadcast android.provider.telephone.sms_ Received 3. Call getextras() of intent inside onReceive method to get PDUs field and get an object [], where each element is a byte []. 4. Create smsmessage object through createfrompdu method of smsmessage class 5. Get sender number, SMS content, sending time and other information from smsmessage object 6. Need to receive SMS permission:

Example:

detailed list

Monitor SMS broadcast:

Monitor outgoing calls

1. Define the broadcast receiver to receive android.intent.action.new_ OUTGOING_ Call 2. Permission required

life cycle

1. The life cycle of the broadcast receiver is very short. It is created when the broadcast is received and destroyed after the onReceive () method ends. 2. Do not do some time-consuming work in the broadcast receiver, otherwise the application no response error dialog box will pop up. 3. It is best not to create a sub thread in the broadcast receiver to do time-consuming work, Because after the broadcast receiver is destroyed, the process becomes an empty process, which is easy to be killed by the system. 4. The time-consuming and long work is best completed in the service

I hope this article will help you in Android programming.

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