Broadcast Mechanism of Android learning notes (1)

Source: Internet
Author: User

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 ..

 

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.