Use of broadcast in Android

Source: Internet
Author: User

The broadcast message mechanism is used for system-level message notification, each application can register the broadcast of interest, and the method of receiving the broadcast is defined in the broadcast receiver (broadcast).

Broadcasts can be divided into standard broadcasts and ordered broadcasts.

Methods for registering broadcasts can be dynamically registered (registered in code) and statically registered (registered in the manifest file)

We can also publish our own defined broadcasts and receive them.

1. Dynamic Registration

The process of dynamic registration:

1) First define a broadcast receiver, inherit broadcast. Override the OnReceive () method.

2) Create the Intentfilter class and add the broadcast you want to listen to.

3) Create a custom broadcast receiver and register it. (typically implemented in the OnCreate () method)

4) Cancel the registration of the broadcast receiver. (typically implemented in the OnDestroy () method.)

Here's a look at the implementation code.

  

1  Public classMainactivityextendsappcompatactivity {2 3 4 Intentfilter Intentfilter;5 mybroadcastreceiver Mybroadcastreceiver;6 7 @Override8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); One  AIntentfilter =NewIntentfilter (); -Intentfilter.addaction ("Android.net.conn.CONNECTIVITY_CHANGE"); -  theMybroadcastreceiver =Newmybroadcastreceiver (); - Registerreceiver (mybroadcastreceiver,intentfilter); -     } -  +  -  +  A @Override at     protected voidOnDestroy () { -         Super. OnDestroy (); - Unregisterreceiver (mybroadcastreceiver); -  -     } -  in     classMybroadcastreceiverextendsbroadcastreceiver{ -  to @Override +          Public voidOnReceive (Context context, Intent Intent) { -Toast.maketext (Context, "Network is changed", Toast.length_long). Show (); the         } *     } $}
View Code2. Static Registration

The static registration process is simpler than dynamic registration and requires only 2 steps

1) define a recipient class yourself, inheriting broadcast.

2) Add the corresponding broadcast to be accepted in the manifest file (androidmanifest.xml) file.

The specific code is as follows

Java code

1  Public class extends broadcastreceiver {2    @Override3public     void OnReceive (Context context, Intent Intent) {45        Toast.maketext (Context, "power is Connected", Toast.length_long). Show (); 6     }7 }
Powerconnreceiver

Code in the Androidmanifest.xml

1     <Application2         Android:allowbackup= "true"3 Android:icon= "@mipmap/ic_launcher"4 Android:label= "@string/app_name"5 Android:theme= "@style/apptheme" >6         <Activity7             Android:name=". Mainactivity "8 Android:label= "@string/app_name" >9             <Intent-filter>Ten                 <ActionAndroid:name= "Android.intent.action.MAIN" /> One  A                 <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> -             </Intent-filter> -  the  -         </Activity> -  -         <receiverAndroid:name=". Powerconnreceiver "> +             <Intent-filter> -                 <ActionAndroid:name= "ANDROID. INTENT. ACTION. Action_power_connected "/> +             </Intent-filter> A         </receiver> at     </Application>
View Code

We can also define our own broadcasts and send them. Communication between different processes can be broadcast, of course, this is not advocated.

  

  

Use of broadcast in Android

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.