Simple example of broadcast send and accept in Android

Source: Internet
Author: User

Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.example.broadcast "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbackup=" True "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/app Theme "> <activity android:name=" com.example.broadcast.MainActivity "android:label=" @st Ring/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/        > <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver android:name= ". Myreceiver "> <intent-filter > <action android:name=" com.example.Broadcast "/> </intent-filter> </receiver> </application></manifest> 


Activity_main.xml

<relativelayout xmlns: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 "    tools:context= ". Mainactivity ">    <button         android:id=" @+id/sendbroad "        android:layout_width=" Match_parent        " android:layout_height= "90DP"        android:text= "send broadcast"/></relativelayout>


Mainactivity

Package Com.example.broadcast;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 Mainactivity extends Activity {    private Button SENDBT;        @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        SENDBT = (Button) Findviewbyid (r.id.sendbroad);                Sendbt.setonclicklistener (New Onclicklistener () {            @Override public            void OnClick (View v) {                Intent Intent = New Intent ();                Intent.setaction ("Com.example.BROADCAST");                Intent.putextra ("msg", "This is the message sent by the broadcast");                Sendbroadcast (intent);}}        );}            }

Myreceiver

Package Com.example.broadcast;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.widget.toast;public class Myreceiver extends Broadcastreceiver {    @Override Public    void OnReceive (context context, Intent Intent) {        Toast.maketext (context, "action bit received by Intent:" + Intent.getaction () + "\ n message content is:" +intent.getstringextra ("msg"), Toast.length_long). Show ();    }}

Effect




Simple example of broadcast send and accept in Android

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.