Android Broadcast Broadcastreceiver

Source: Internet
Author: User

Broadcast in 1.Android is divided into two main forms

1) Standard broadcast

is a fully asynchronous broadcast that, after broadcast, all broadcast receivers receive broadcasts almost at the same time, so they are not sequential and therefore more efficient but also mean

cannot be truncated.

2) Orderly broadcast

is a synchronous broadcast that, after a broadcast is issued, only one broadcast receiver receives the broadcast at the same time, and the receiver does not continue to pass the broadcast until it plays the internal logic. So the broadcast has

High priority receivers receive broadcasts first and can truncate subsequent broadcasts.

How to register a broadcast in 2.Android

To accept a broadcast, you need to register the broadcast that the broadcast receiver is interested in.

1) Static Registration

Registered in Androidmanifest.xml, the program does not start or can receive a broadcast, ANDROID:NAME specifies a broadcast receiver, and the action tag specifies the broadcast to receive. The priority of the broadcast can be

The Intent-filter field is set in the properties

1 <receiverAndroid:name=". Mysmsbroadcastreceiver ">2     <Intent-filterandroid:priority= "+">3         <ActionAndroid:name= "Android.intent.action.BOOT_COMPLETED"/>4     </Intent-filter>5 </receiver>    

2) Dynamic Registration

Registered in the code, the dynamic registration of the broadcast receiver must be unregistered, to be able to dynamically register and unregister the broadcast receiver, control flexibility, but it must be started after the program can be started. Equally wide

The priority of the broadcast can be set in the code.

1  Public classTestactivityextendsactivity{2     Privatemysmsbroadcastreceiver Mbroadscatreceiver;3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_test);7         //Dynamic registration of broadcasts8Mbroadscatreceiver=NewMysmsbroadcastreceiver ( This,NewHandler ());9Intentfilter filter=Newintentfilter ();TenFilter.setpriority (100); OneFilter.addaction ("Android.provider.Telephony.SMS_RECEIVED"); A Registerreceiver (mbroadscatreceiver,filter); -     } - @Override the     protected voidOnDestroy () { -         Super. OnDestroy (); -         //Cancel registration, dynamic registration must not forget to cancel the registration - Unregisterreceiver (mbroadscatreceiver); +     } -}

3. Send Custom Broadcasts

1) Send standard broadcast

Intent intent=New Intent ("Com.example.mybroadcast"); Sendbroadcast (Intent);

2) Send an ordered broadcast

Intent intent=New Intent ("Com.example.mybroadcast"); Sendorderedbroadcast (Intent,null) ;

The second argument is a permission-related string. For a received broadcast, if you do not want to continue the delivery, you can perform the Abortbroadcast () method in the broadcast sink to stop the continuation of the broadcast.

4. Local broadcasts.

1) Local Broadcast registration

The broadcasts previously mentioned are all global broadcasts, that is, all applications on the same device can receive this broadcast. If you want to send a broadcast that can only be received by this application, it will be broadcast locally. Local wide

The only difference between the broadcast registration and the general broadcasting registration is that the local broadcast manager is required to register.

1 //Dynamic registration of broadcasts2Mbroadscatreceiver=Newmysmsbroadcastreceiver ();3Mlocalbroadcastmanager=localbroadcastmanager.getinstance ( This);//Get Instance4Intentfilter filter=NewIntentfilter ();5Filter.setpriority (100);6Filter.addaction ("Android.provider.Telephony.SMS_RECEIVED");7Mlocalbroadcastmanager.registerreceiver (Mbroadscatreceiver,filter);//Local Registration

2) local broadcast cancellation.

Mlocalbroadcastmanager.unregisterreceiver (Mbroadscatreceiver); // Local Cancel Registration

3) Advantages of local broadcast.

A) data will not leak, do not worry about the existence of security risks;

b) Sending a local broadcast is more efficient than sending a global broadcast.

Note: Local broadcasts cannot be registered statically in the Androidmanifest.xml file. Do not include too much code logic and any time-consuming operation in the OnReceive () method of the broadcast receiver, in the broadcast forbid

The thread is allowed to open. Broadcasting is more of a role to open other components, such as creating a status bar notification, opening a service, and so on.

Android Broadcast Broadcastreceiver

Related Article

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.