Android Learning Note (14)--Custom broadcast

Source: Internet
Author: User

This series is the "first line of Android Code" Learning notes, if there are errors and omissions, please correct me!

In addition to receiving system broadcasts through a broadcast receiver, we can also send custom broadcasts in the application. Let's try each one to send custom standard broadcasts and ordered broadcasts.

One, standard broadcast:

1) before sending the broadcast, we still need to define a broadcast receiver to prepare to receive this broadcast:

 1  public  class  mybroadcastreceiver   Broadcastreceiver { 2  Span style= "color: #000000;" > @Override  3  public  void   OnReceive (context context, Intent Intent) { 4  toast.maketext (context, "What to Do"  5   6  }
View Code

We create a new mybroadcastreceiver inherits from Broadcastreceiver, and sends a text when it receives the message.

2) Register this broadcast receiver in the manifest file:

1 <receiverAndroid:name=". Mybroadcastreceiver ">2             <Intent-filter>3                 <ActionAndroid:name= "Com.example.broadcasttest." My_broadcast "/>4             </Intent-filter>5         </receiver>
View Code

Add the above code under the Application tab.

3) Modify the layout file: Add a button to the layout file to trigger;
4) Modify the main activity code:

1 @Override2     protected voidonCreate (Bundle savedinstancestate) {3         Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.activity_main);5Button Button =(Button) Findviewbyid (R.id.button);6Button.setonclicklistener (NewView.onclicklistener () {7 @Override8              Public voidOnClick (View v) {9Intent Intent =NewIntent ("Com.example.broadcasttest.MY_BROADCAST");Ten Sendbroadcast (intent); One             } A         }); -}
View Code

The results of the operation are as follows:
  

Second, orderly broadcasting:
In order to test the orderly broadcast, we create a new BROADCASTTEST2, code and broadcast similar, the Mybroadcastreceiver class to Anotherbroadcastreceiver class, corresponding to the toast of the text modified, Easy to distinguish. The main activity code is as follows:

1 @Override2     protected voidonCreate (Bundle savedinstancestate) {3         Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.activity_main);5Button Button =(Button) Findviewbyid (R.id.button);6Button.setonclicklistener (NewView.onclicklistener () {7 @Override8              Public voidOnClick (View v) {9Intent Intent =NewIntent ("Com.example.broadcasttest.MY_BROADCAST");TenSendorderedbroadcast (Intent,NULL); One             } A         }); -}
View Code

As you can see, sending an ordered broadcast requires only one line of code to change and the Sendbroadcast () method is changed to the Sendorderedbroadcast () method. The Sendorderedbroadcast () method receives two parameters, the first parameter is still intent, and the second parameter is a permission-related string, where null is passed.

Since it is orderly, it means that we can specify the order of the broadcast receivers, where it is possible to change the order of the receivers by adding the following code to the manifest:

1 <receiverAndroid:name=". Anotherbroadcastreceiver " >2             <Intent-filterandroid:priority= "+" >3                 <ActionAndroid:name= "Com.example.broadcasttest.MY_BROADCAST"/>4             </Intent-filter>5         </receiver>
View Code

We set the priority for the broadcast receiver via the Android:priority property, and the broadcast receiver with higher priority can receive the broadcast first. The program runs as follows:

If the Abortbroadcast () method is called at the end of the OnReceive () method in a high-priority broadcast receiver, the broadcast is truncated and the subsequent broadcast receivers will no longer be able to receive the broadcast.

Abortbroadcast ();

Iii. use of local broadcasts:
The broadcasts we send and receive all belong to the system global broadcast, that is, the broadcast can be received by any other application, and we can also receive broadcasts from any other application. This can easily lead to security issues, such as data leaks. In order to be able to solve the broadcast security problem simply, Android introduced a set of local broadcast mechanism, broadcast using this mechanism can only be passed inside the application, and the broadcast receiver can only receive broadcasts from this application.

Local broadcast is primarily a localbroadcastmanager used to manage broadcasts and provides a way to send broadcasts and register broadcast receivers:

1  Public classMainactivityextendsActivity {2     PrivateIntentfilter Intentfilter;3     Privatelocalreceiver Localreceiver;4     PrivateLocalbroadcastmanager Localbroadcastmanager;5 @Override6     protected voidonCreate (Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9Localbroadcastmanager = Localbroadcastmanager.getinstance ( This);Ten //Get Instance OneButton Button =(Button) Findviewbyid (R.id.button); AButton.setonclicklistener (NewView.onclicklistener () { - @Override -              Public voidOnClick (View v) { theIntent Intent =NewIntent ("Com.example.broadcasttest.LOCAL_BROADCAST"); -Localbroadcastmanager.sendbroadcast (Intent);//send a local broadcast -             } -         }); +Intentfilter =NewIntentfilter (); -Intentfilter.addaction ("Com.example.broadcasttest.LOCAL_BROADCAST"); +Localreceiver =Newlocalreceiver (); A localbroadcastmanager.registerreceiver (Localreceiver, intentfilter); at //registering a local broadcast listener -     } - @Override -     protected voidOnDestroy () { -         Super. OnDestroy (); - Localbroadcastmanager.unregisterreceiver (localreceiver); in     } -     classLocalreceiverextendsBroadcastreceiver { to @Override +          Public voidOnReceive (Context context, Intent Intent) { -Toast.maketext (Context, "received local broadcast", the toast.length_short). Show (); *         } $     }Panax Notoginseng}
View Code

In fact, this is basically the same as the code that we learned earlier about dynamically registering broadcast receivers and sending broadcasts. It is only now that an instance of it is obtained by Localbroadcastmanager's getinstance () method, and then the Localbroadcastmanager is called when registering the broadcast receiver. The Registerreceiver () method, which is called the Localbroadcastmanager Sendbroadcast () method when sending the broadcast, is nothing more. Here we send a Com.example.broadcasttest.LOCAL_BROADCAST broadcast in the button's click event and then receive the broadcast in the Localreceiver.

End.

Android Learning Note (14)--Custom broadcast

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.