1. Register in Androidmanifest.xml
<receiverAndroid:name=". Myreceiver "> <Intent-filter> <ActionAndroid:name= "Myaction"/> </Intent-filter> </receiver>
2. Registering in the activity
Newnew intentfilter (); Filter.addaction ("myaction"); Registerreceiver ( receiver, filter);
Comparison of two registered Broadcastreceiver methods:
Now that we understand the two methods of registering Broadcastreceiver , consider the scenarios in which the two methods apply:
I. The first method of registration ensures that thebroadcastreceiver is always active after the application is installed, usually to monitor changes in the state of the system, such as the power of the phone,wifi the status of the NIC (of course, monitoring these things also depends on the needs of the software). For such broadcastreceiver, usually after a specific system event is generated, the corresponding operation, such as the wifi network card when opened, Give the user a hint;
Ii. the second method of registration is more flexible than the first, so that registered Broadcastreceiver are typically used to update The state of the UI. In general, when an Activity is started, this method is used to register broadcastreceiver, and once the broadcast event is received, it can be The OnReceive method updates the current Activity 's controls. However , it should be noted that if the Activity is not visible, you should call the unregisterreceiver method to cancel the registration;
Function:
I. Registerreceiver (Receiver,filter);
This function is to register a broadcastreceiver in the application, the function receives two parameters, the first parameter is required to register the broadcastreceiver Object, the second one is a intentfilter. The first parameter is very easy to understand, and the second parameter defines which Intent to trigger the registered broadcastreceiver object. Similar to what was explained earlier
the role of <intent-filter> tags;
II. UNREGISTERRECEIVER (receiver);
This method is very simple and is used to unbind the broadcastreceiver state. Once this is done, the Broadcastreceiver of the response will no longer receive the Intent that the system broadcasts .
Here is the source code demo:
Acitivity:
PackageCom.example.broadcast;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {Button mybt; Intent Intent; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); MYBT=(Button) Findviewbyid (R.ID.MYBT); Mybt.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub /*There are two ways to register Broadcastreceiver: one is to use the <receiver> tag Life register statically in the Androidmanifest.xml and use the <intent-filte in the tag. R> Label Settings filter. Another way is to dynamically define and set up a Intentfilter object in the code, then Context.registerreceiver () method where it needs to be registered, and call Context.unregiste if it is canceled. Rreceiver () method. If the Broadcastreceiver context object that is registered dynamically is destroyed, Broadcastreceiver will automatically unregister. (Special note, some may require background monitoring, such as SMS messages)*///myreceiver receiver = new Myreceiver ();//intentfilter filter = new Intentfilter ();//filter.addaction ("myaction");//Registerreceiver (receiver, filter); //Create a Intent objectIntent =NewIntent (); //set the Action property of IntentIntent.setaction ("Myaction"); //Join InformationIntent.putextra ("msg", "Ordinary broadcast"); //Transfer InformationSendbroadcast (Intent); } }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true; }}
Broadcastreceiver:
PackageCom.example.broadcast;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.widget.Toast; Public classMyreceiverextendsbroadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {//TODO auto-generated Method StubToast.maketext (Context, "Action is:" +intent.getaction ()+ "\ n msg is:" +intent.getstringextra ("MSG")), Toast.length_long). Show (); }}
Service Send broadcast:
New Intent (); Intent.setaction ("Com.he.chat.receiveNewMessage"); // Create filter conditions Intent.putextra ("Key", "newmessage"); // Passing Parameters Intent.putextra ("Sender", Msg.getsender ()); // Passing Parameters Sendbroadcast (Intent);
Manifest:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.broadcast"Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK Android:minsdkversion= "8"android:targetsdkversion= "/>" <Application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <receiver android:name= ". Myreceiver "> <intent-filter > <action android:name=" myaction "/> </i Ntent-filter> </receiver> <Activity Android:name= "Com.example.broadcast.MainActivity"Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter& Gt </activity> </application></manifest>