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