Android four components of the Broadcastreceiver

Source: Internet
Author: User

Today, I looked back at my previous study notes, a summary of the broadcastreceiver, for the newly-introduced friends to review and reference.

Broadcastreceiver is a global listener for the Android system that enables communication between different components.

Create your own broadcast class

Package com.example.yummyhttputil; Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.os.IBinder; public class Myboradcastreceiver extends Broadcastreceiver {     /**     * @param args */public    static void Main (string[] args) {        //TODO auto-generated method stub     }     @Override public    void OnReceive (Context Context, Intent Intent) {        //TODO auto-generated method stub             }     @Override public    ibinder Peekservice ( Context Mycontext, Intent service) {        //TODO auto-generated method stub        return Super.peekservice (Mycontext, Service);    } }


Register a broadcast       Static registration
<receiver android:name= ". Myboradcastreceiver ">              <intent-filter>                  <action android:name=" Android.intent.action.MYBROADCAST_RECEIVER "/>                  <category android:name=" Android.intent.category.DEFAULT "/>              </intent-filter>  </receiver>  
after configuring the above information, as long as the Android.intent.action.MY_BROADCAST address of the broadcast, Myreceiver can receive. Note that this method of registration is permanent, that is, when the application is closed, if there is broadcast information, Myreceiver will also be called by the system automatically run.
Dynamic Registration
Myboradcastreceiver receiver = new Myboradcastreceiver ();           Intentfilter filter = new Intentfilter ();  Filter.addaction ("Android.intent.action.MYBROADCAST_RECEIVER");        Registerreceiver (receiver, filter);  
Note that Registerreceiver is a method in the Android.content.ContextWrapper class, and both activity and service inherit contextwrapper, so they can be called directly. In the actual application, we register a broadcastreceiver in activity or service, when the activity or service is destroyed if not de-registration, the system will report an exception, indicating whether we forgot to release the registration. So, remember to perform the de-registration operation in a specific place:
@Override  protected void OnDestroy () {      Super.ondestroy ();      Unregisterreceiver (receiver);  }  


Send broadcast
public void Send (view view) {      Intent Intent = new Intent ("Android.intent.action.MYBROADCAST_RECEIVER");      Intent.putextra ("msg", "Hello yummylau!");      Sendbroadcast (Intent);  


Types of broadcastsGeneral Broadcast (normal broadcast)Ordinary broadcasts are completely asynchronous for multiple receivers, and usually each recipient does not have to wait for the broadcast to be received, and the recipient has no effect on each other. For this broadcast, the receiver cannot stop the broadcast, that is, the receiving action of the other receivers cannot be blocked.
Orderly Broadcast (Ordered broadcast) ordered broadcast is special, it is sent only to the higher priority recipients, and then by the higher priority recipients to the lower priority recipients, high priority recipients have the ability to terminate the broadcast.
set up three broadcast acceptance classes to determine who will consume the broadcast first by setting a priority
<receiver android:name= ". Firstreceiver "> <intent-filter android:priority=" "> <action android:name=" android.intent.act Ion. Mybroadcast_receiver "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-fil ter> </receiver> <receiver android:name= ". Secondreceiver "> <intent-filter android:priority=" 999 "> <action android:name=" android.intent.act Ion. Mybroadcast_receiver "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-fil ter> </receiver> <receiver android:name= ". Thirdreceiver "> <intent-filter android:priority=" 998 "> <action android:name=" android.intent.acti On. Mybroadcast_receiver "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-fil Ter> </receiver> 

The <intent-filter> of three receivers has a android:priority attribute and decreases in turn. This property has a range of 1000 to 1000, and the higher the value, the greater the precedence.
Send an ordered broadcast

public void Send (view view) {      Intent Intent = new Intent ("Android.intent.action.MYBROADCAST_RECEIVER");      Intent.putextra ("msg", "Hello yummylau!");      Sendorderedbroadcast (Intent, "Scott.permission.MY_BROADCAST_PERMISSION");  

note that when sending an ordered broadcast using the Sendorderedbroadcast method, a permission parameter is required, and if NULL indicates that the recipient is not required to declare the specified permission, and if it is not NULL, the recipient is required to declare the specified permission to receive the broadcast. This is done from a security perspective, such as the system's text message is the form of orderly broadcast, an application may be the ability to intercept spam messages, when the message arrives it can first receive the text message broadcast, if necessary, stop the broadcast transmission, such software must declare the permission to receive text messages.
So we define a permission in Androidmainfest.xml:
<permission android:protectionlevel= "Normal"              
then declare this permission
<uses-permission android:name= "Scott.permission.MYBROADCAST_RECEIVER_PERMISSION"/>  
if the first broadcast needs to add a message to the second broadcast, you can add it in OnReceive ()
Bundle bundle = new bundle (); Bundle.putstring ("AddString", "I am the message added"); Setresultextras (bundle);
and the second broadcast class takes out
Bundle bundle = Getresultextras (true); String add = bundle.getstring ("addstring");
Cancel to continue delivery of the broadcast
Abortbroadcast ();  












Android four components of the Broadcastreceiver

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.