Mainactivity as follows:
Package Cc.testbroadcasta;import Android.os.bundle;import Android.app.activity;import android.content.Intent;/** * Demo Description: * Use Androidmanifest.xml to register the broadcast receiver */public class Mainactivity extends Activity {@Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Intent Intent = new Intent ();//Set Actionintent.setaction ("Cc.test");//Carry Data Intent.putextra ("name", "Stephen Chow Chi"), Intent.putextra ("number", 9527);//Send Broadcast sendbroadcast (intent);}}
Broadcastreceivertest as follows:
Package Cc.testbroadcasta;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public class Broadcastreceivertest extends Broadcastreceiver {@Overridepublic void OnReceive ( Context context, Intent Intent) { String Name=intent.getstringextra ("name"); int Number=intent.getintextra ("number", 0); System.out.println ("Received broadcast name=" +name+ ", number=" +number);} public static void Main (string[] args) {}}
Main.xml as follows:
<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 " > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Use Androidmanifest.xml to register the broadcast receiver" android:layout_centerinparent= "true"/></ Relativelayout>
Androidmanifest.xml as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Cc.testbroadcasta "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk Android:minsdkversion= "ten" android:targetsdkversion= "/> <application android:allowbackup=" Tru E "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/appthe Me "> <activity android:name=" cc.testbroadcasta.MainActivity "android:label=" @string/ap P_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> & Lt;/activity> <!--register a broadcast recipient--<receiver android:name= "cc.testbroadcasta.BroadcastReceiverTest "> <intent-filter > <action android:name= "Cc.test"/> </intent-filter> </receiver> </ Application></manifest>
Broadcastreceiver broadcast Recipient (ii)--using Androidmanifest.xml to register a broadcast receiver