"Turn" Android broadcastreceiver Introduction

Source: Internet
Author: User

This paper mainly introduces Broadcastreceiver concept, use, life cycle, security, classification, special broadcastreceiver (local, sticky, orderly, sticky and orderly broadcasting).
Example code see BROADCASTRECEIVERDEMO, Example apk see: trineaandroiddemo.apk.

1. Concept introduction and the difference between the two ways of registration

Broadcastreceiver as one of the four components of Android, unlike activity, there is no interface to display. Broadcastreceiver consists of two concepts, the broadcast sender and the broadcast receiver (receiver), where the broadcast actually refers to the intent, the program can send itself to receive the broadcast itself, or accept the broadcast of the system or other applications or send broadcasts to other applications.

The sender can send broadcasts via a similar context.sendbroadcast interface, the recipient is dynamically registered via Context.registerreceiver () or in the Androidmanifest.xml file via <receiver> label static registration, after the registration is completed, when the sender sends a broadcast, the system will send the broadcast (Intent) and all the registered eligible recipients (receiver) in the system to match the Intentfilter, If the match succeeds then the corresponding receiver's onreceive function is executed , and the matching rules are found in the matching rules of intent and intentfilter.

the differences between registerreceiver dynamic registration and statically registering broadcasts via <receiver> tags are as follows:

A. Calls to Bindservice ,<receiver> registered broadcasts that do not exist at the end of the onreceive, so it is not possible to pass the result asynchronously to itself. such as Bindservice and can only use StartService, if you want to interact with the service can use Peekservice.

B. Manual control . Registerreceiver for dynamic registration, you can manually register or unregister;<receiver> label for static registration, by the system when the automatic scanning registration, so can not manually control, power-on has been running.

C. Resource consumption is different . Registerreceiver can be controlled manually, so proper registration and cancellation can save system resources,<receiver> label system has been in effect since boot.

D. The validity period is different . Broadcastreceiver registered with Registerreceiver is "destroyed" by the context object to which it was registered or called Unregisterreceiver method, and through <receiver > tag Registration broadcastreceiver is valid as long as the application has not been deleted.

E. The invocation license for the Registerreceiver function is different . Broadcastreceiver registered by Registerreceiver can call the Registerreceiver function of a context again in its onreceive function, and by <receiver> The broadcastreceiver of the label registration does not allow the Registerreceiver function of a context to be called again.

F. Different usage conditions . broadcasts that you send and receive can be registered through Registerreceiver, and the reception of the system's popular broadcasts is usually registered with the <receiver> tag.

2. Use Example

public class Broadcastreceiverdemo extends Activity {private final static String Action_send = "Com.trinea.android.dem    O.broadcastreceiverdemo.sendbroadcast ";    Private final static String Msg_key = "MSG";    Private Mybroadcastreceiver receiver;    Private Button sendbtn;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.broadcast_receiver_demo);        Receiver = new Mybroadcastreceiver ();        SENDBTN = (Button) Findviewbyid (r.id.sendbroadcast);                Sendbtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {                Sendbtn.settext ("in-send");                Sendbtn.setclickable (FALSE);                Intent i = new Intent (action_send);                I.putextra (Msg_key, "Good sound begins soon");            Sendbroadcast (i);    }        });       } @Override public void OnPause () {super.onpause (); Unregisterreceiver (receiver);        } @Override public void Onresume () {super.onresume ();    Registerreceiver (receiver, new Intentfilter (Action_send)); } public class Mybroadcastreceiver extends Broadcastreceiver {@Override public void OnReceive (Context Co            ntext, Intent Intent) {sendbtn.settext ("send broadcast");            Sendbtn.setclickable (TRUE);        Toast.maketext (Context, Intent.getstringextra (Msg_key), Toast.length_short). Show (); }    }}

The content of R.layout.broadcast_receiver_demo is a simple button with ID sendbroadcast

From the code above, we can see

A. The new broadcastreceiver only needs to inherit Broadcastreceiver and rewrite the Onreceiver function, plus its own processing logic.

B. Registration of broadcasts through the Registerreceiver, cancellation of the registration of broadcasts through Unregisterreceiver, broadcast by Sendbroadcast.

Registration and de-registration broadcasts in the Onresume and OnPause functions can effectively save system consumption. If you want the broadcast to continue running, you can register in the activity's OnCreate function and unregister in the Ondestrory function.

The mybroadcastreceiver can also be registered statically in the Androidmanifest.xml file, so the program is installed and running. For example, if you want to receive a message when it arrives, the following:

<receiver android:name= "Mybroadcastreceiver" >    <intent-filter>        <action android:name= " Android.provider.Telephony.SMS_RECEIVED "/>    </intent-filter></receiver>

"Turn" Android broadcastreceiver Introduction

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.