How to register Broadcastreceiver

Source: Internet
Author: User

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

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.