Broadcastreceive is one of Android's four core components, essentially a global listener for listening to the system's global broadcast messages.
It is used to receive the program issued by the broadcast Intent, the program starts it only two steps:
(1) Create a receivecastreceiver that needs to be started intent
(2) invoke the Sendbroadcast () or Sendorderedbroastcast () method of the context to start the specified broadcastreceiver
When an application emits a broadcast intent, all broadcastreceive that match that intent may be started
Routines:
Testactivity.java Main Page activity
Import Android.app.activity;import android.content.intent;import Android.os.bundle;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;public class Testactivity extends Activity {private Button Sendbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Sendbutton = (Button) Findviewbyid (R.id.sendbutton); Sendbutton.setonclicklistener ( New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setaction (intent.action _edit); TestActivity.this.sendBroadcast (intent);}});}}
Testreceiver.java rewriting Broadcastreceiver's OnReceive method
Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public class Testreceiver extends Broadcastreceiver {public testreceiver () {System.out.println ("testreceive");} @Overridepublic void OnReceive (context context, Intent Intent) {System.out.println ("OnReceive");}}
Andriodmanifest.xml configuration Specifies the intent that the Broadcastreceiver can match
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" MARS.TESTBC "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk Android oid:minsdkversion= "8" android:targetsdkversion= "/>" <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" ; <activity android:name= "mars.testbc.TestActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <categ Ory android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver android:name = ". Testreceiver "> <intent-filter > <action android:name=" Android.intent.action.EDIT "/> </intent-filter> </receiver> </application></manifest>
Result: Click on the button on the main page (two times)
When the OnReceive () method finishes executing, the Broadcastreceive instance is destroyed.
Broadcastreceive Learning Routines