热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidSMSabortsystemdefaultnotify

简单的说下abortBroastcast()的作用,发送广播时有senbroadcast和sendOrderBroadcast()

简单的说下abortBroastcast()的作用,发送广播时有senbroadcast和sendOrderBroadcast(),后一个是带优先级的,即在receiver的,其中数值越大,优先级越高,就会先收到广播,如果不想比他优先级低的人受到这个信息,那就在onReceiver()方法的最后加上这个abort,这样就中断了广播的继续传递

 
               //优先级一定要加入
                 
           
 
       
   


public class SmsMessageReceiver extends BroadcastReceiver {

 private final static String TAG = "SmsMessageReceiver";
 @Override
 public void onReceive(Context context, Intent intent) {
   Log.v(TAG, ">>>>>>>onReceive start"); 
         // 第一步、获取短信的内容和发件人  

         boolean flags_filter = false;  
         StringBuilder body = new StringBuilder();// 短信内容  
         StringBuilder number = new StringBuilder();// 短信发件人  
         Bundle bundle = intent.getExtras(); 
         Log.i(TAG,"receiver action = "+ intent.getAction());
         if (bundle != null) { 
             Object[] _pdus = (Object[]) bundle.get("pdus"); 
             SmsMessage[] message = new SmsMessage[_pdus.length]; 
             for (int i &#61; 0; i <_pdus.length; i&#43;&#43;) { 
                 message[i] &#61; SmsMessage.createFromPdu((byte[]) _pdus[i]); 
             } 
             for (SmsMessage currentMessage : message) { 
                 body.append(currentMessage.getDisplayMessageBody()); 
                 number.append(currentMessage.getDisplayOriginatingAddress()); 
             } 
             String smsBody &#61; body.toString(); 
             String smsNumber &#61; number.toString(); 
            
             Log.e(TAG, "smsBody&#61;"&#43;smsBody&#43;",\n smsNumber&#61;"&#43;smsNumber);
             Toast.makeText(context, "smsBody&#61;"&#43;smsBody&#43;",\n smsNumber&#61;"&#43;smsNumber, Toast.LENGTH_SHORT).show();
            
             if (smsNumber.contains("&#43;86")) { 
                 smsNumber &#61; smsNumber.substring(3); 
             } 
             // 第二步:确认该短信内容是否满足过滤条件   
             if (smsNumber.equals("xxxxxxxxx")) {// 屏蔽某個號碼發來的信息   
                 flags_filter &#61; true; 
                 Log.v(TAG, "sms_number.equals(10086)"); 
             } 
             // 第三步:取消  
             if (flags_filter) { 
                 this.abortBroadcast();  //This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast


             } 
         } 
         Log.v(TAG, ">>>>>>>onReceive end"); 


 }

}


推荐阅读
author-avatar
经来泓
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有