Android broadcast Summary

Source: Internet
Author: User

Android broadcast Summary

1. The sendBroadcast method sends a 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 present. 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. icationicationmanager; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. OS. build;/*** broadcast Life Cycle onReceive () from start to end * @ author YangQuanqing yqq **/@ SuppressLint ("NewApi ") public class MyReceiver extends BroadcastReceiver {// Notification management instance private icationicationmanager icationmanager; // create a Notification instance private Notification. builder builder; @ TargetApi (Build. VERSION_CODES.HONEYCOMB) @ Overridepublic void onReceive (Context context, Intent intent) {// TODO Auto-generated method stub // obtain icationicationmanager instance icationicationmanager = (icationicationmanager) context. getSystemService (Context. NOTIFICATION_SERVICE); // instantiate the instance builder that creates a Notification = new Notification. builder (context); // set the notification builder. setContentTitle ("SMS notification"); builder. setContentText ("hello"); builder. setSmallIcon (R. drawable. ic_launcher); // construct a notification icationicationmanager. policy (10086, builder. build ());}}


2. Send ordered 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) {// broadcast Intent intent (); Intent. putExtra ("name", "Li Hua"); intent. setAction ("com. yqq. mutilBrocast "); sendBroadcast (intent) ;}}); // sends an 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 ordered broadcast sendOrderedBroadcast (intent, null) ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. 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 void onReceive (Context context, Intent intent) {Log. I ("BroadCast1 <", intent. getStringExtra ("name"); // abortBroadcast (); // terminate 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 {@ Overridepublic void onReceive (Context context, Intent intent) {Log. I ("BroadCast2 <", intent. getStringExtra ("name"); abortBroadcast (); // If the broadcast is not received when the ordered broadcast is terminated }}

/** *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 {@Overridepublic void onReceive(Context context, Intent intent) {Log.i("BroadCast3<<<", intent.getStringExtra("name"));}}

 
             
              
                              
          
         
               
                              
          
         
               
                              
          
     
 
 


The following demo shows how to broadcast an application in actual development:

Package com. example. yqqmobilesafe. service; import java. util. list; import android. app. notification; import android. app. icationicationmanager; 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 interception broadcast receiver 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 ()");} // register the broadcast mer ER = 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 the broadcast receiver unregisterReceiver (mterer); mReceiver = null; super. onDestroy ();} // The class StopStrangerSmsReceiver extends BroadcastReceiver {private String sender; // the sender's number is private String body; // SMS content @ Overridepublic void onReceive (Context context, Intent intent) {Object [] objs = (Object []) intent. getExtras (). get ("pdus"); for (Object obj: objs) {SmsMessage smsmsmessage = smsMessage. createFromPdu (byte []) obj); // obtain the message sender's number sender = smsMessage. getOriginatingAddress (); body = smsMessage. getMessageBody (); new AsyncTask
 
  
() {List
  
   
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
   
    
() {@ Overrideprotected Void doInBackground (Void... params) {RubishSmsInfo sms = new RubishSmsInfo (); sms. setPhoneNumber (sender); sms. setSmsContent (body); // store stranger 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); icationicationmanager nm = (icationicationmanager) getSystemService (NOTIFICATION_SERVICE); Intent intent = new Intent (StopStrangerPhoneNumberSmsService. this, RubishSmsActivity. class); PendingIntent pi = PendingIntent. getActivity (StopStrangerPhoneNumberSmsService. this, 0, intent, 0); // send an interception Notification notification Notification = new Notification. builder (getApplicationContext ()). setAutoCancel (true ). setTicker ("blocking strange text messages "). setSmallIcon (com. example. yqqmobilesafe. r. drawable. notif_icon_sms ). setContentText ("blocking strange text messages" ).setContentIntent(pi).build();nm.notify(100,notification);}}.execute();super.onPostExecute(result);}}.exe cute ();}}}}
   
  
 



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.