Broadcastreceiver broadcast Recipient (ii)--using Androidmanifest.xml to register a broadcast receiver

Source: Internet
Author: User

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

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.