Android Common Components Broadcast introduction

Source: Internet
Author: User

I. INTRODUCTION of Broadcast

Broadcast is one of the four components of Android. Can be divided into:

1, the ordinary broadcast to send a broadcast, all listening to the broadcast of the broadcast receiver can listen to change the broadcast.

2, asynchronous broadcast when the intent after processing, still exist, this time Registerreceiver (Broadcastreceiver,intentfilter) can also receive his value, until you remove it, you can not pass the results to the next recipient, The broadcast cannot be terminated.

3. Ordered broadcasts receive broadcasts in the priority order of the recipient, which is declared in priority in Intent-filter, between 1000 and 1000, and the higher the value, the greater the priority. The continuation of the broadcast intent can be terminated. The recipient can tamper with the content.

Second, the broadcasting broadcast realization process

1. Registration

A, configuration file registration

1 <!--registering a custom static broadcast receiver -  2 <receiverAndroid:name= "Com.example.androidtest.broadcast.MyReceiver">3     <Intent-filter>4        <ActionAndroid:name= "Myaction"/>5    </Intent-filter>6</receiver>

B, Code Registration

1 New myreceiver (); 2 New Intentfilter (); 3 filter.addaction ("myaction"); 4 registerreceiver (myreceiver,filter);

2. Send Broadcast

1 New Intent (); 2 intent.setaction ("myaction"); 3 Intent.putextra ("msg", "This is the message sent by the broadcast"); 4 Sendbroadcast (intent);

3, the implementation of the receiving class

Inherit Broadcastreceiver and override the OnReceive method.

 1  public  class  Myreceiver extends   Broadcastreceiver  Span style= "color: #008080;"     >2  { 3  public  void   OnReceive (context context, Intent Intent)  4   { 5  Toast.maketext (Context, "broadcast message is:" + Intent.getstringextra ("msg" ), Toast.lengt H_short). Show ();  6   7  8 } 

Iii. How to implement the inner class as a broadcast receiving class

1. Registration

A, configuration file registration

1 <!--register a custom static broadcast receiver--  2 <receiver android:name= "com.example.androidtest.broadcast.broadcasttest$ Myreceiver ">  <!--Note Inner class notation: Ordinary Class $ inner class-->3 <intent-filter >4    <action android:name=" myaction "/ >5    </intent-filter>6</receiver>

B, Code Registration

1 Myreceiver myreceiver = new Myreceiver (), 2 intentfilter filter = new Intentfilter (); 3 Filter.addaction ("Myactio n "); 4 registerreceiver (Myreceiver,filter);  

2. Send Broadcast

1 Intent Intent = new Intent (), 2 intent.setaction ("Myaction"), 3 Intent.putextra ("MSG", "This is a message sent by the broadcast"); 4 Sendbroadcast (intent);  

3, the implementation of the receiving class

Note: If you register in the configuration file, the class must be declared as static, otherwise it cannot be found, if the registration section in the code is omitted from the static

1 public static class Myreceiver extends{3 public     void    {5         toast.maketext (context, "broadcast message is:" + Intent.getstringextra ("MSG" }7 8}  

Iv. use

Boot, lock screen, low power ...

Five, attention

Life cycle is only about 10 seconds, if within the onreceive () do more than 10 seconds of things, will be error.

Each time the broadcast arrives, the Broadcastreceiver object is recreated, and the OnReceive () method is called, and the object is destroyed when it finishes executing. When the OnReceive () method is not completed within 10 seconds, Android will consider the program unresponsive. So in the broadcastreceiver can not do some more time-consuming operation, the side will pop up the ANR (Application noresponse) dialog box

Part of the content is from: http://yangguangfu.iteye.com/blog/1063732

Android Common Components Broadcast introduction

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.