There are two ways to register Broadcastreceiver:
1. Registering in the code of the application
If a broadcastreceiver is used to update the UI, this method is typically used to register
Register Broadcastreceiver when activity starts and unregister after activity is not visible
Registered Broadcastreceiver:registerreceiver (receiver,filter);
Unregister broadcastreceiver:unregisterreceiver (receiver);
2. Registration in Androidmanifest.xml
Registering broadcastreceiver in this way is always active even if the application is not started or killed, Broadcastreceiver can also receive broadcast messages
<receiver android:name= ". Testreceiver "> <intent-filter> <action android:name=" Android.intent.action.PICK "/> </intent-filter> </receiver>
Routines: Registering in the application's code:
Testbc2activity.java
public class Testbc2activity extends Activity {private Button Registerbutton = null;private button Unregisterbutton = null ;p rivate smsreceiver smsreceiver = null;private static final String sms_action = "Android.provider.Telephony.SMS_ RECEIVED "; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Registerbutton = (Button) Findviewbyid (r.id.register); Unregisterbutton = (Button) Findviewbyid (R.id.unregister); Registerbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//generates a Broiadcastreceiver object smsreceiver = new Smsreceiver ();//generates a Intentfilter object intentfilter filter = New Intentfilter ();//Add a actionfilter.addaction (sms_action) for intentfilter;// Register the Broadcastreceiver object in the System TestBC2Activity.this.registerReceiver (Smsreceiver,filter);}); Unregisterbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {// Unlocks the Broadcastreceiver object's registration TestBC2Activity.this.unregisterReceivER (smsreceiver);}});}}
Smsreceiver.java
public class Smsreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {Syste M.OUT.PRINTLN ("Receive Message");//Accept Data Bundle bundle in Intent object = Intent.getextras ();//The Bundle object has a property named PDUs, The value of this property is an Object array object[] Myobjpdus = (object[]) bundle.get ("PDUs");//Create an array of smsmessage type smsmessage[] message = new Smsmessage[myobjpdus.length]; System.out.println (message.length); for (int i = 0; i < myobjpdus.length; i++) {// Create Smsmessage objects using objects in Object array Message[i] = SMSMESSAGE.CREATEFROMPDU ((byte[]) myobjpdus[i]);// Call the Getdisppalymessagebody () method of the Smsmessage object to get the contents of the Message System.out.println (Message[i].getdisplaymessagebody ());}}}
This broadcast receive is a test to receive SMS, so requires Mainfest file to allow
<uses-permission android:name= "Android.permission.RECEIVE_SMS" ></uses-permission>
First Press "Register" button, register Broadcastreceiver, that is, the binding listener, and then send a text message to the simulation
Results:
Note:
Texting the simulator using emulator Control
How to register Broadcastreceiver