Broadcast of Android Study Notes (2)

Source: Internet
Author: User

Broadcastreceiver is used to listen to broadcast events (itent)

To achieve this goal, we must register broadcastreceiver in two ways:

<1> ApplicationProgramCode.

This kind of registration method is flexible, so we want to register at what time and when to close it.

Register broadcastreceiver:

Registerreeiver (receiver, filter)

Cancel broadcastreceiver:

Unregisterreeiver (receiver)

If a broadcastreciver is used to update the UI, this method is usually used for registration. The broadcastreciver is registered when the activity is started, and the registration is canceled when the activity is invisible.

Step: 1 generate a broadcastreceiver object

2. generate an intentfilter object

3. Add an action for the intentfilter object

<Textarea class = "Java" style = "width: 512px; Height: 329px; margin: 1.11111px; "Name =" code "readonly> class registerreceiverlistener implements onclicklistener {</P> <p> Public void onclick (view V) {<br/> // generate a broiadcastreceiver object <br/> smsreceiver = new smsreceiver (); <br/> // generate an intentfilter object <br/> intentfilter filter = new intentfilter (); <br/> // Add an action for intentfilter <br/> filter. addaction (sms_action); <br/> // register the broadcastreceiver object to the system. <br/> testbc2activity. this. registerreceiver (smsreceiver, filter ); <br/>}</P> <p> class unregisterreceiverlistener implements onclicklistener {</P> <p> Public void onclick (view V) {<br/> // cancel the registration of the broadcastreceiver object <br/> testbc2activity. this. unregisterreceiver (smsreceiver); <br/>}</P> <p >}</textarea><Textarea class = "Java" style = "height: 34px; width: 156px; margin: 1.11111px;" name = "code" readonly> smsreceiver. Java </textarea>Package Mars. testbc2; </P> <p> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. telephony. smsmessage; </P> <p> public class smsreceiver extends broadcastreceiver {</P> <p> @ override <br/> Public void onreceive (context, intent) {<br/> // todo auto-generated method stub <br/> system. out. println ("Receive message"); </P> <p> // accept data in the intent object <br/> bundle = intent. getextras (); <br/> // in the bundle object, there is an attribute named PDUS, the value of this attribute is an object array <br/> object [] myobjpdus = (object []) bundle. get ("PDUS"); <br/> // create an array of the smsmessage type <br/> smsmessage [] messages = new smessage [myobjpdus. length]; <br/> system. out. println (messages. length); <br/> for (INT I = 0; I <myobjpdus. length; I ++) <br/>{< br/> // use the object in the object array to create a smsmessage object <br/> messages [I] = smsmessage. createfrompdu (byte []) myobjpdus [I]); <br/> // call the getdisppalymessagebody () method of the smsmessage object to obtain the message content. <br/> system. out. println (messages [I]. getdisplaymessagebody (); <br/>}< br/> try {<br/> thread. sleep (30*1000); <br/> system. out. println ("-----------------------------"); <br/>} catch (interruptedexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p >}< br/>

 

<2> Register in androidmanifest. xml

<Cycler Android: Name = ". testreceiver "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. edit "/> <br/> </intent-filter> <br/> </receiver>

Registering using this method has the following features:

Applications registered in this way are closed in time, and broadcastreceiver will still receive a widely broadcasted object, that is, no matter the application is closed or opened, it is in an active state, all Accept Broadcast events.
Some applications need this feature.

For example, develop an application to detect the power consumption of mobile phones.

We cannot always ask the application to open. We hope that the application will be shut down in time, and we can also detect the power consumption of our mobile phone.

However, this method is not suitable if we develop applications to update mobile phone controls. Only when we see activity, we update the control to make sense.

If you do not see the activity, it will still be updated. In addition to power consumption and memory consumption, there is no benefit at all. Therefore, the first method is required.

Related Article

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.