Four components in Android are activity, service, broadcast, and content provider. Intent is an abstract description of actions and actions. It is responsible for message transmission between programs between components. The broadcast Receiver component provides a mechanism to broadcast intent as a message and respond to it by all programs interested in it.
In Android, broadcast is a very useful function. Broadcast can notify other broadcasts to accept this event. Such as insufficient power supply and poor signal.
Next I made a simple demo, first look at activity (broadcasttest. Java)
Package COM. android. broadcasttest; </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 broadcasttest extends activity {<br/> Public static final string new_lifeform_detected = <br/> "com. android. broadcasttest. new_lifeform "; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> button btn0 = (button) findviewbyid (R. id. btn0); <br/> btn0.setonclicklistener (New onclicklistener () {<br/> Public void onclick (view v) {<br/> intent it = new intent (new_lifeform_detected ); <br/> sendbroadcast (it); <br/>}< br/>}); <br/>}< br/>}
A button is generated in this activity. When the button is pressed, a broadcast is sent through sendbroadcast.
Let's look at the broadcast receiver code:
Package COM. android. broadcasttest; </P> <p> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. util. log; <br/> Import android. widget. toast; </P> <p> public class mybroadcastreceiver extends broadcastreceiver {<br/> Public static final string burn = <br/> "com. paad. alien. action. burn_it_with_fire "; </P> <p> Public mybroadcastreceiver () {<br/> log. V ("broadcast_tag", "mybroadcast"); <br/>}< br/> @ override <br/> Public void onreceive (context, intent) {<br/> // todo auto-generated method stub <br/> toast. maketext (context, "successfully received broadcast:", toast. length_long ). show (); <br/>}</P> <p>}
In onreceive (), an action is performed when the broadcast is received.
We also need to register the handler in androidmanifest. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. android. broadcasttest "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". broadcasttest "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <Cycler Android: Name = ". mybroadcastreceiver "> <br/> <intent-filter> <br/> <action Android: Name =" com. android. broadcasttest. new_lifeform "/> <br/> </intent-filter> <br/> </javaser> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "8"/> <br/> </manifest>
The operator's Action defines the broadcast that the receiver can accept.