Broadcast simple practice of Android components

Source: Internet
Author: User

As one of the four components of Android, there is no reason not to introduce the Broadcast,broadcast Chinese simple translation is broadcast, the former group of Zhejiang University of the cheerleading, square dance aunt, which aunt and students heard from the horn and sound is the source of events, The recipient is the mother and the students, the program comes from practice, Android broadcast, there are event broadcasters also have event receivers, broadcast is a widely used mechanism for transmitting information between applications, Broadcastreceiver is to send out Broadcast a class of components that are filtered to accept and respond, that is, the receiver. After you have finished the basic concepts, you can begin the basic practice:

two ways to implement custom broadcasts

Two kinds of implementation, one is static, one is dynamic, first look at the static implementation process:

Define a Bookbroadcast receiver (there are several decisions to add that are required for system broadcast):

public class Bookbroadcast extends Broadcastreceiver {private String tag = "Bookbroadcast"; @Overridepublic void OnReceive (context context, Intent Intent) {//TODO auto-generated method stubstring actionstring = Intent.getaction (); String name = Intent.getstringextra ("name"), if (Actionstring.equals (intent.action_time_changed)) {LOG.I (tag, "Name:") + name + "--action:" + actionstring+ "--man, Time changed ~");} else if (actionstring.equals (intent.action_battery_changed)) {LOG.I (tag, "name:" + name + "--action:" + actionstring+ "-- Man, the charge has changed ~ "); else {LOG.I (tag, "name:" + name + "--action:" + actionstring);}}

Look at the app page:

Static broadcasts correspond to the first button, click events:

Intent Intent = new Intent ("com.google.broadcast.BookBroadCast.static"); Intent.putextra ("Name", "static broadcast"); Sendbroadcast (Intent);

Static broadcasts need to be registered in Androidmanifest.xml:

   <receiver android:name= "Com.google.broadcast.BookBroadCast" >            <intent-filter >                <action Android:name= "com.google.broadcast.BookBroadCast.static"/>                   <action android:name= " Android.intent.action.TIME_SET "/>            </intent-filter>        </receiver>    

The results are as follows:

Dynamic calls are not required to register in Androidmanifest.xml, dynamically generate filter settings in the program action, OnStart registration in the activity, OnStop termination:

@Overrideprotected void OnStop () {//TODO auto-generated method Stubsuper.onstop (); Unregisterreceiver (bookreceiver);} @Overrideprotected void OnStart () {//TODO auto-generated method Stubsuper.onstart (); Intentfilter intentfilter = new Inte Ntfilter (); Intentfilter.addaction ("Com.google.broadcast.BookBroadCast.dymanic"); bookreceiver = new Bookbroadcast ( ); Registerreceiver (Bookreceiver, intentfilter);}

Dynamic invoke event, corresponding to the second button:

Intent dintent = new Intent ("Com.google.broadcast.BookBroadCast.dymanic");d Intent.putextra ("Name", "dynamic Broadcast"); Sendbroadcast (dintent);

The results are as follows:

two ways to call system broadcasts

About the various events of the system broadcast on the Internet, I don't need to post it here, first to implement the third and fourth button events, the third is the change of the system event, and the fourth is the change of the battery power:

The constant of the third action has a discrepancy with the name of the intent enumeration:

    <action android:name= "Android.intent.action.TIME_SET"/>

This is the time to call:

Intent sintent = new Intent (intent.action_time_changed); Sintent.putextra ("Name", "unprotected system broadcast"); Sendbroadcast (sintent) ;

The results are as follows:

This is not really a big problem, and then according to the static registration method can not handle the fourth battery power problem, first look at the fourth is how to achieve:

Intentfilter systemintentfilter = new Intentfilter (intent.action_battery_changed); systembookreceiver = new Bookbroadcast (); Registerreceiver (Systembookreceiver, Systemintentfilter);

The first thing you can see is not static registration, you need to register dynamically, and then do not need to call sendbroadcast, the result is as follows:

This problem resolution is one hours high, the final Google API found the corresponding explanation:

Address: Http://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_TICK

Encounter problems found that Google is the most reliable, but also searched a lot of Chinese articles, do not believe that no one encountered the pit, hey, Saturday to see the blog is Love learning children, weekend happy ~

Broadcast simple practice of Android components

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.