Android:broadcastreceiver's practice uses

Source: Internet
Author: User

1  PackageCom.example.broadcastdemo1;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 Importandroid.content.Intent;6 ImportAndroid.content.IntentFilter;7 ImportAndroid.view.Menu;8 ImportAndroid.view.View;9 Ten  Public classMainactivityextendsActivity { One     PrivateMyBroadcastReceiver2 Mbr2; A     PrivateMyBroadcastReceiver3 Mbr3; - @Override -     protected voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Intentfilter Intentfilter =NewIntentfilter ("Bc_one"); -MBR2 =NewMyBroadcastReceiver2 (); + registerreceiver (MBR2, Intentfilter);  -     } +      A      Public voidDoClick (View v) { at         Switch(V.getid ()) { -          CaseR.id.normal_broadcast: -Intent Intent =NewIntent (); -Intent.putextra ("msg", "This is a regular broadcast"); -Intent.setaction ("Bc_one"); - Sendbroadcast (intent); in              Break; -          CaseR.id.ordered_broadcast: toIntent Intent2 =NewIntent (); +Intent2.putextra ("msg", "This is an orderly broadcast"); -Intent2.setaction ("Bc_one"); theSendorderedbroadcast (Intent2,NULL); *              Break; $          CaseR.id.sticky_broadcast:Panax NotoginsengIntent Intent3 =NewIntent (); -Intent3.putextra ("msg", "This is an orderly broadcast"); theIntent3.setaction ("Bc_three"); + Sendstickybroadcast (INTENT3); AIntentfilter Intentfilter =NewIntentfilter ("Bc_three"); theMbr3 =NewMyBroadcastReceiver3 (); + registerreceiver (Mbr3, intentfilter); -              Break; $         } $     } -      - //@Override the //protected void OnDestroy () { - //        //TODO auto-generated Method StubWuyi //Super.ondestroy (); the //Unregisterreceiver (MBR2); - //Unregisterreceiver (MBR3); Wu //    } -}
1  PackageCom.example.broadcastdemo1;2 3 ImportAndroid.content.BroadcastReceiver;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 ImportAndroid.os.Bundle;7 ImportAndroid.util.Log;8 9  Public classMyBroadcastReceiver2extendsbroadcastreceiver{Ten  One @Override A      Public voidOnReceive (Context context, Intent Intent) { -         //TODO auto-generated Method Stub -String str = Intent.getstringextra ("MSG"); theLOG.I ("info", "I am 2!") The message is: "+str); -Bundle bundle =NewBundle (); -Bundle.putstring ("Test", "processed data"); - Setresultextras (bundle); + //abortbroadcast (); -     } +  A}
1  PackageCom.example.broadcastdemo1;2 3 ImportAndroid.content.BroadcastReceiver;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 ImportAndroid.os.Bundle;7 ImportAndroid.util.Log;8 9  Public classMybroadcastreceiverextendsbroadcastreceiver{Ten  One @Override A      Public voidOnReceive (Context context, Intent Intent) { -         //TODO auto-generated Method Stub -String str = Intent.getstringextra ("MSG"); theLOG.I ("info", "I am 1!") The message is: "+str); -Bundle bundle = Getresultextras (true); -String str2 = bundle.getstring ("Test"); -LOG.I ("info", "What is this?") "+str2); +     } -  +}
1  PackageCom.example.broadcastdemo1;2 3 ImportAndroid.content.BroadcastReceiver;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 ImportAndroid.util.Log;7 8  Public classMyBroadcastReceiver3extendsbroadcastreceiver{9 Ten @Override One      Public voidOnReceive (Context context, Intent Intent) { A         //TODO auto-generated Method Stub -LOG.I ("Info", "received an asynchronous broadcast!")); -     } the  -}
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3      Package= "Com.example.broadcastdemo1"4Android:versioncode= "1"5Android:versionname= "1.0" >6 7<uses-SDK8Android:minsdkversion= "8"9Android:targetsdkversion= "/>"Ten<uses-permission android:name= "Android.permission.BROADCAST_STICKY"/> One  A<Application -Android:allowbackup= "true" -android:icon= "@drawable/ic_launcher" theAndroid:label= "@string/app_name" -Android:theme= "@style/apptheme" > -<Activity -Android:name= "Com.example.broadcastdemo1.MainActivity" +Android:label= "@string/app_name" > -<intent-filter> +<action android:name= "Android.intent.action.MAIN"/> A  at<category android:name= "Android.intent.category.LAUNCHER"/> -</intent-filter> -</activity> -<receiver -Android:name= "Com.example.broadcastdemo1.MyBroadcastReceiver" > -<intent-filter > in<action android:name= "Bc_one"/> -</intent-filter> to</receiver> +</application> -  the</manifest>

Android:broadcastreceiver's practice uses

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.