Broadcast is divided into ordinary broadcast, orderly broadcast, asynchronous broadcast, asynchronous broadcast and ordinary broadcast similar, broadcast can not be terminated and processed, ordered broadcast termination can be processed, and three kinds of high-level first receive broadcast.
Target effect:
Click the first button to send a normal broadcast, the second button is to send an ordered broadcast, because the normal broadcast can not be processed, so the first button click after processing the result is null.
1.activity_main.xml page to place two buttons.
Activity_main.xml page:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity "><button android:id=" @+id/btsendone " android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_ Centerhorizontal= "true" android:onclick= "DoClick" android:text= "send a plain broadcast"/><button Android : id= "@+id/btsendtwo" android:layout_width= "match_parent" android:layout_height= "Wrap_content " Android:layout_alignparentleft= "true" android:layout_below= "@+id/btsendone" android:onclick= "DoClick " android:text=" sends an orderly broadcast "/></relativelayout>
2. Create a new Broad.java page as the first page to receive a broadcast. Broad.java page:
Package Com.example.broad;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.os.bundle;import Android.util.log;public class Broad extends BroadcastReceiver {@Overridepublic void OnReceive (Context content, Intent Intent) {String S=intent.getstringextra ("Sendone"); LOG.I ("Mainactivity", "Receive Message 1:s=" +s);/* Process broadcast */bundle bundle=new Bundle (); Bundle.putstring ("Test", "Broadcast processed data"); Setresultextras (bundle);//whether to get return value}}
3. Create a new Broadsecond.java page as the second page to receive the broadcast. Broadsecond.java page:
Package Com.example.broad;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.os.bundle;import Android.util.log;public class BroadSecond extends broadcastreceiver{@Overridepublic void OnReceive (Context content, Intent Intent) {String S=intent.getstringextra (" Sendone "); LOG.I ("Mainactivity", "received message 2:s=" +s);//abortbroadcast (); Intercept broadcasts, only function on ordered broadcasts bundle Bundle=getresultextras (TRUE); String s2=bundle.getstring ("test"); LOG.I ("mainactivity", "resulting processing result: s2=" +s2);}}
The 4.mainactivity.java page is used to send broadcasts. Mainactivity.java page:
Package Com.example.broad;import Android.os.bundle;import Android.app.activity;import android.content.Intent; Import Android.content.intentfilter;import android.view.menu;import Android.view.view;import Android.widget.Button ;p ublic class Mainactivity extends Activity {private Button btsendone,btsendtwo;private Intent intentone,intenttwo; Private broad one; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); getId ();//Get Control idregister ();//register broadcast}private void GetId () {btsendone= (Button) Findviewbyid (R.id.btsendone); btsendtwo= (Button) Findviewbyid (r.id.btsendtwo);} public void DoClick (view view) {switch (View.getid ()) {case r.id.btsendone://sends a plain broadcast intentone=new Intent (); Intentone.putextra ("Sendone", "This is a general broadcast"), Intentone.setaction ("Main");//filter Sendbroadcast (intentone);//Send break; Case R.id.btsendtwo:intenttwo=new Intent (); Intenttwo.putextra ("Sendone", "This is an orderly broadcast"); Intenttwo.setaction ("Main") ;//Filter Sendorderedbroadcast (inTenttwo,null);//Send, parameter two for permission break;}} /* Dynamically registering a broadcast receiver with priority higher than static registration, only valid when the program is running */private Void Register () {intentfilter intentfilter=new intentfilter ("Main"); one= New Broad (); Registerreceiver (One,intentfilter);} /* Dynamically registered broadcasts need to be destroyed */@Overrideprotected void OnDestroy () {Super.ondestroy (); Unregisterreceiver (one);}}
5. The broadcast needs to register, the registration has the dynamic and the static two ways, the first broadcast is uses the dynamic registration, the second broadcast uses the static registration, therefore needs to register on the Androidmanifest.xml page. Androidmanifest.xml page:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.broad "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "android:targetsdkversion="/> <uses-permission android:name= "Android.permi Ssion. Broadcast_sticky "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launcher " Android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:n Ame= "com.example.broad.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "android.i Ntent.category.LAUNCHER "/> </intent-filter> </activity> <!--static Register receiver-- <receiver Android:name= "COm.example.broad.broadSecond "> <intent-filter> <!--android:priority=" "is a priority, high priority first- Line-to <action android:name= "Main"/> </intent-filter> </receiver> </application></manifest>
6. The same level of broadcast, dynamic registration priority is higher than static registration, static registration can also be customized to set priority, the value range is 1-1000.
<intent-filter android:priority= "> <!--android:priority=" "is priority, high priority first---- < Action android:name= "Main"/> </intent-filter>
7. Finally, you need to add permissions.
8. Run to show the target effect.
Usage of Android-broadcastreceiver Broadcast