Android Basics: Broadcast Broadcast simple example

Source: Internet
Author: User

Main.xml

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.mytest.testbroadcast.MainActivity" >    <EditTextAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:hint= "@string/param_input"Android:id= "@+id/param_input"/>    <ButtonAndroid:id= "@+id/send_button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_torightof= "@id/param_input"Android:text= "@string/send_button"Android:onclick= "Sendbroadcast" />             <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@id/param_input"Android:text= "@null"Android:id= "@+id/view_result"       />        </Relativelayout>
View Code

Androidmainfest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.mytest.testbroadcast"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <!--registering a Broadcast receive class -        <receiverAndroid:name=". Mybroadcastreceiver ">            <Intent-filter>                <ActionAndroid:name= "Com.mytest.testbroad.intent"/>            </Intent-filter>                    </receiver>    </Application></Manifest>
View Code

Mainactivity.java Send Broadcast

 PackageCom.mytest.testbroadcast;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Paramtext; PrivateTextView Resultview; Private FinalString action_intent_test= "Com.mytest.testbroad.intent"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Paramtext= (EditText) This. Findviewbyid (R.id.param_input); Resultview= (TextView) This. Findviewbyid (R.id.view_result); }        /*** Send broadcast button onclick event *@paramView*/     Public voidsendbroadcast (view view) {String param=Paramtext.gettext (). toString (); //Create send IntentIntent Intent =NewIntent ( This.        Action_intent_test); //Binding ParametersIntent.putextra ("User_input", param); //Send broadcast         This. Sendbroadcast (Intent); Resultview.settext ("Send broadcast succeeded, parameter value:" +param); }    }
View Code

Mybroadcastreceiver Receiving broadcasts

 PackageCom.mytest.testbroadcast;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.widget.Toast; Public classMybroadcastreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {//TODO auto-generated Method StubString user_input = Intent.getstringextra ("User_input"); Toast.maketext (Context,"Receive the broadcast, get the parameter value" +user_input,toast.length_short). Show (); }}
View Code

Summary of CASE implementation:
1, define action, and set up send intent, call Sendbroadcast method send broadcast;
2. Create a broadcast receive class. Class to inherit Broadcastreceiver, and rewrite the OnReceive method;
3. Register the broadcast receiving object in the Androidmanifest.xml and add the action in the Intent-filter.

Android Basics: Broadcast broadcast simple instance

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.