Android Broadcast Summary

Source: Internet
Author: User

1, the Sendbroadcast method sends the broadcast.

Package Com.example.android_broadcast_normal;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.menu;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;public class Mainactivity extends Activity {private Button btn; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btn= (Button) This.findviewbyid (r.id.button1);//Send Broadcast Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {Intent intent=new Intent (mainactivity.this,myreceiver.class); Sendbroadcast (Intent);}});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

/** *version: *author:yangquanqing *data: */package com.example.android_broadcast_normal;import Android.annotation.suppresslint;import Android.annotation.targetapi;import Android.app.notification;import Android.app.notificationmanager;import Android.content.broadcastreceiver;import Android.content.Context;import Android.content.intent;import android.os.build;/** * Broadcast life cycle onreceive () from start to finish * @author yangquanqing YQQ * */@ Suppresslint ("Newapi") public class Myreceiver extends Broadcastreceiver {//Notification management instance private Notificationmanager notificationmanager;//Create a Notifier instance private Notification.builder Builder; @TargetApi (build.version_codes. Honeycomb) @Overridepublic void onreceive (context context, Intent Intent) {//TODO auto-generated method stub// Get Notificationmanager Instance notificationmanager= (Notificationmanager) Context.getsystemservice (Context.NOTIFICATION_ SERVICE);//Instantiate Create Notifier instance builder=new notification.builder (context);//Set Notification builder.setcontenttitle ("SMS Notification"); Builder.setcontenttext ("Hello"); Builder.setsmallicoN (r.drawable.ic_launcher);//Build a notification notificationmanager.notify (10086,builder.build ());}} 


2. Send sequence Broadcast

Package Com.example.android_brocast_order;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.menu;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;public class Mainactivity extends Activity {private Button btn1,btn2; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); btn1= (Button) This.findviewbyid (R.id.button1); btn2= (Button) This.findviewbyid (R.id.button2); Btn1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//Multiple broadcast receivers send broadcast intent Intent=new Intent (); Intent.putextra ("name", "Li Hua"), Intent.setaction ("Com.yqq.mutilBrocast"); Sendbroadcast (Intent);}); /Send ordered broadcast Btn2.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Create intent Intent Intent=new Intent (); Intent.putextra ("Name", "Android"); Intent.setaction ("Com.yqq.mutilBrocast");// Send an ordered broadcast sendorderedbroadcast (intent,null);}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

/** *version: *author:yangquanqing *data: */package com.example.android_brocast_order;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.util.log;/** * @author yangquanqing yqq * */public class BroadCast1 extends Broadcastreceiver {@Overridepublic Voi D onreceive (context context, Intent Intent) {log.i ("broadcast1<<<", Intent.getstringextra ("name"));// Abortbroadcast ();//Terminating broadcast}}


/** *version: *author:yangquanqing *data: */package com.example.android_brocast_order;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.util.log;/** * @author yangquanqing yqq * */public class CopyOfBroadCast2 extends Broadcastreceiver {@Overridepub LIC void OnReceive (context context, Intent Intent) {log.i ("broadcast2<<<", Intent.getstringextra ("name")); Abortbroadcast ();//If the broadcast is terminated, the subsequent broadcasts are not received}}

/** *version: *author:yangquanqing *data: */package com.example.android_brocast_order;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.util.log;/** * @author yangquanqing yqq * */public class CopyOfBroadCast3 extends Broadcastreceiver {@Overridepub LIC void OnReceive (context context, Intent Intent) {log.i ("broadcast3<<<", Intent.getstringextra ("name"));}}

 <receiver android:name= "Com.example.android_brocast_order. BroadCast1 "> <!--Set priority integer the greater the priority the greater the precedence--<intent-filter android:priority=" 10000 " > <action android:name= "com.yqq.mutilBrocast"/> </intent-filter> </rece iver> <receiver android:name= "Com.example.android_brocast_order. CopyOfBroadCast2 "> <intent-filter android:priority=" > <action Android             Oid:name= "Com.yqq.mutilBrocast"/> </intent-filter> </receiver> <receiver Android:name= "Com.example.android_brocast_order. CopyOfBroadCast3 "> <intent-filter android:priority=" 9 "> <action Androi D:name= "Com.yqq.mutilBrocast"/> </intent-filter> </receiver> 


The application of broadcasting in actual development is shown in the following demo:

Package Com.example.yqqmobilesafe.service;import Java.util.list;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.app.service;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.os.asynctask;import Android.os.ibinder;import Android.telephony.smsmessage;import Android.util.log;import Com.example.yqqmobilesafe.rubishsmsactivity;import Com.example.yqqmobilesafe.contactprovider.contactinfoprovider;import Com.example.yqqmobilesafe.db.dao.rubishsmsinfodao;import Com.example.yqqmobilesafe.domain.contactinfo;import com.example.yqqmobilesafe.domain.rubishsmsinfo;/** * Unfamiliar SMS Interception service * @author YQQ * */public class Stopstrangerphonenumbersmsservice extends Service {Private Boolean d=true;private stopstrangersmsreceiver mreceiver;/ /Stranger SMS Intercept Broadcast recipient public Stopstrangerphonenumbersmsservice () {//TODO auto-generated constructor stub} @OverridepUblic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn null;} @Overridepublic void OnCreate () {if (D) {log.i ("Stopstrangerphonenumbersmsservice", "OnCreate ()");} Registered broadcast mreceiver=new stopstrangersmsreceiver (); Intentfilter filter=new intentfilter (); Filter.setpriority ( Integer.max_value); Filter.addaction ("Android.provider.Telephony.SMS_RECEIVED"); Registerreceiver (Mreceiver, filter); Super.oncreate ();} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {if (D) {log.i (" Stopstrangerphonenumbersmsservice "," Onstartcommand ");} Return Super.onstartcommand (Intent, flags, Startid);} @Overridepublic void OnDestroy () {if (D) {log.i ("Stopstrangerphonenumbersmsservice", "OnDestroy ()");} Cancel broadcast recipient unregisterreceiver (Mreceiver); Mreceiver=null;super.ondestroy ();} Stranger SMS Intercept Broadcast recipient class Stopstrangersmsreceiver extends Broadcastreceiver{private string sender;//sender number private string body;//SMS Content @overridepublic void OnReceive (context context, Intent Intent) {object[] Objs = (object[]) Intent.getextras (). Get ("PDUs"); for (Object obj:objs) {smsmessage smsmessage = SMSMESSAGE.CREATEFROMPDU ((byte[)) obj) ///Get the sender's number of the SMS sender = Smsmessage.getoriginatingaddress (); BODY = Smsmessage.getmessagebody (); New Asynctask<void,void,void> () {list<contactinfo> infos=null; @Overrideprotected Void Doinbackground ( Void ... params) {Contactinfoprovider contactinfoprovider=new Contactinfoprovider (Getapplicationcontext ()); infos= Contactinfoprovider.getsystemcontactinfos (); return null;} @Overrideprotected void OnPostExecute (void result) {if (infos==null) {//toast.maketext (Getapplicationcontext (), Duration) return;} for (ContactInfo Info:infos) {if (Info.getphonenumber (). Equals (sender)) {return;}} Abortbroadcast (); new asynctask<void,void,void> () {@Overrideprotected void Doinbackground (void ... params) { Rubishsmsinfo sms=new rubishsmsinfo (); Sms.setphonenumber (sender); sms.setsmscontent (body);// Storing strangers ' text messages in the database table Rubishsmsinfodao rubishsmsinfodao=new Rubishsmsinfodao (Getapplicationcontext ()); RubiShsmsinfodao.addsmsinfos (SMS); return null;} @Overrideprotected void OnPostExecute (void result) {//TODO auto-generated method Stubsuper.onpostexecute (Result); Notificationmanager nm= (Notificationmanager) Getsystemservice (notification_service); Intent intent=new Intent ( Stopstrangerphonenumbersmsservice.this,rubishsmsactivity.class); Pendingintent pi=pendingintent.getactivity (stopstrangerphonenumbersmsservice.this, 0, intent, 0);// Send an interception notice notification notification=new Notification.builder (Getapplicationcontext ()). Setautocancel (True). Setticker ("Intercept to unfamiliar text messages"). Setsmallicon (com.example.yqqmobilesafe.r.drawable.notif_icon_sms). Setcontenttext ("Intercept to unfamiliar text messages"). Setcontentintent (PI). build (); Nm.notify (100,notification);}}. Execute (); Super.onpostexecute (result);}}. Execute ();}}}



Android Broadcast Summary

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.