The broadcast receiver (broadcastreceiver) is used to asynchronously receive the broadcast intent. The broadcast intent is sent by calling context. sendbroadcast (), context. sendorderedbroadcast (), or context. sendstickybroadcast. Generally, a broadcast intent can be received by multiple broadcast recipients subscribed to this intent.
Broadcast Mechanism:
The method to implement a broadcast receiver is as follows:
Interface:
Package Mars. testbc; </P> <p> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; </P> <p> public class testactivity extends activity {<br/>/** called when the activity is first created. */<br/> private button sendbutton; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. testreceiver); <br/> sendbutton = (button) findviewbyid (R. id. sendbutton); <br/> sendbutton. setonclicklistener (New broadcastlistener (); <br/>}< br/> class broadcastlistener implements onclicklistener {</P> <p> Public void onclick (view V) {<br/> intent = new intent (); <br/> intent. setaction (intent. action_edit); <br/> testactivity. this. sendbroadcast (intent); <br/>}</P> <p >}< br/>}
Step 1: to implement a broadcast mechanism, you must implement a class that inherits from broadcastreceiver and overwrites the onreceive () method.
Package Mars. testbc; </P> <p> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; </P> <p> public class testreceiver extends broadcastreceiver {</P> <p> Public testreceiver () {<br/> system. out. println ("testreceiver"); <br/>}< br/> @ override <br/> Public void onreceive (context, intent) {<br/> system. out. println ("onreceive"); <br/>}< br/>
The implementation of event response should be implemented in the onreceive method.
Step 2: subscribe to the broadcast intent you are interested in. There are two subscription methods:
First: UseCodeSubscribe
Intentfilter filter = new intentfilter ("android. provider. telephony. sms_received ");
Incomingsmsreceiver extends ER = new incomingsmsreceiver ();
Registerreceiver (receiver, filter );
Type 2: register in the <Application> node in the androidmanifest. xml file:
<Intent-filter> is a filter.
When sending a broadcast, if the action of a broadcastreceiver matches the action, start and accept the broadcast.
<Cycler Android: Name = ". testreceiver "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. pick "/> <br/> </intent-filter> <br/> </Cycler>
Lifecycle:
Each time the broadcast arrives, the broadcastreceiver object is re-created and the onreceive () method is called. After execution, the object is destroyed.
When the onreceive () method is not executed within 10 seconds, Android considers thisProgramNo response.
Broadcastreceiver does not support time-consuming operations. The ANR (application No
Response) dialog box ..