Two registration methods and comparison of Broadcast Receiver in applications

Source: Internet
Author: User

After the system or user application sends a broadcast, the broadcast receiver that meets the conditions will receive the broadcast. Generally, broadcast sending is implemented through the sendBroadcast (Intent intent) (or sendStickyBroadcast) or sendOrderedBroadcast () methods (these two sending Methods correspond to two different broadcasts respectively, one is normal Broadcast and the other is ordered Broadcast.) intent is the Intent to be Broadcast. The so-called conformity condition is that the Broadcast receiver only receives the Intent filtered by the receiver's IntentFilter, therefore, to enable the broadcast receiver to receive a certain intent, you must register the broadcast receiver.

This is easy to understand. Let's briefly introduce the Broadcast Receiver. Broadcast Receiver is one of the four major Android components (Activity, Service, and Content Provider). It is used to listen to the Intent of the broadcast system or user program, it is essentially a global listener of the system (similar to but different from the onXxxxListener). As long as a matched Intent is broadcast, BroadcastReceiver will be activated, therefore, BroadcastReceiver is different from an Activity or Service with a full life cycle (the broadcast receiver has only one life cycle callback function onReceive ). The BroadcastReceiver workflow is as follows: after a system program or user program broadcasts an Intent, the Receiver will be received by the broadcast Receiver that matches the Intent (this broadcast Receiver can be the Receiver obtained by the user by extending the BroadcastReceiver), and then onReceive (Context context, intent intent) method code, you can complete the functions you want to implement here.

Therefore, to make the receiver match a broadcast intent, You need to register the receiver. There are two methods to register a broadcast receiver, which can be found in androidmanifest. in XML, the <receiver ER> label is used for registration. For example, the following registration method indicates that a text message is received, and the system sends a broadcast. This broadcast will be received by the expanded broadcast receiver:

<Cycler Android: Name = "package name. expanded broadcast receiver name "> <intent-filter> <action Android: Name =" android. provider. telephony. sms_received "/> </intent-filter> </Cycler>

However, after you use this registration method to register, even if the program is closed or not enabled, it will receive a matching broadcast. Another more flexible registration method is to register in the Code. This method can be used in similar scenarios. When a condition is met, the broadcast receiver is registered to receive the broadcast, in another condition, the registered broadcast receiver is removed from receiving the broadcast. Specifically, you can use two methods to achieve registration and unregister: registerreceiver (broadcastreceiver receiver, intentfilter filter) and unregisterreceiver (broadcastreceiver ). The method for registering a explorer is as follows:

IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");MyReceiver receiver = new MyReceiver();registerReceiver(receiver, filter);

It should be noted that, in addition to the intentfilter used to filter broadcast, the intentfilter of other components can be dynamically created in the code, and must be registered in androidmanifest. xml.

PS: as we mentioned earlier, broadcastreceiver is essentially a system-level global listener. Therefore, broadcastreceiver provides a new idea for communication between different components. For example, a program has an activity and a service, and the service is started through the startservice () method (the Service also has a startup method started through the bindservice () method, generally, this activity cannot communicate with a service started using the startservice () method, but with the help of broadcastreceiver, the program can implement communication between the two (in essence, communication between components implemented through intent ). For more information about how to use intent to implement communication between components, see my next blog.


References: Lecture on crazy android and instructor Mars

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.