Broadcastreceive Learning Routines

Source: Internet
Author: User

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

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.