android--start Broadcast (broadcast)

Source: Internet
Author: User

1. Define the broadcast component, and the component type inherits the Broadcastreceiver class:

Package Org.lxh.demo;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.widget.toast;public class Mybroadcastreceiverutil extends BroadcastReceiver { Public Mybroadcastreceiverutil () {       System.out.println ("* * * * * Each broadcast instantiates a new broadcast component for operation! ");} @Overridepublic void OnReceive (context context, Intent Intent) {//Note parameter Toast.maketext (context, "broadcast started", Toast.length_ LONG). Show ();}}

2.main.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/mybtn"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Start broadcast"/></linearlayout>

3. Define the activity program to send the broadcast:

Package Org.lxh.demo;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog;import Android.content.dialoginterface;import Android.content.intent;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;public class Hello extends Activity {private Button mybtn = Null;public void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager to use THIS.MYBTN = (Button) Super.findviewbyid (R.ID.MYBTN); This.mybtn.setOnClickListener (new Onclicklistenerimpl ());} Private class Onclicklistenerimpl implements Onclicklistener {public void OnClick (View arg0) {Intent it = new Intent (inten T.action_edit); Hello.this.sendBroadcast (it);}}}

4. Register the broadcast component in the Androidmanifest.xml file:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android     package= "Org.lxh.demo"     android:versioncode= "1"     android:versionname= " 1.0 >    &LT;USES-SDK android:minsdkversion= "Ten"/>    <application      & nbsp android:icon= "@drawable/icon"         android:label= "@string/app_name" >        <activity            android:name= ". Hello "            android:label=" @string/app_name ">          &N Bsp <intent-filter>                <action android:name= " Android.intent.action.MAIN "/>                <category android:name=" Android.intent.category.LAUNCHER "/>            </intent-filter>        </activity>        <strong><span style= "color: #ff0000;" ><receiver            android:name= "Mybroadcastreceiverutil"       & nbsp     android:enabled= "true" >            <intent-filter>    &NB Sp           <action android:name= "Android.intent.action.EDIT"/>            </intent-filter>        </receiver></span></strong>  & nbsp </application></manifest>
5. Run the following example:



android--start Broadcast (broadcast)

Related Article

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.