5.5 短信黑名单拦截

1.在黑名单服务中, 动态代码创建短信广播接受者, 在服务开启时注册短信拦截广播意图过滤器, 在服务销毁时取消广播事件
2. 在广播接受者内部类中,判断来电号码是否在黑名单短信拦截和全部拦截模式中, yes-->终止广播
 
 
 
 
public   class   CallSmsSafeService   extends   Service {

       private   static   final   String   TAG   =   "CallSmsSafeService" ;
       private   TelephonyManager    tm ;
       private   BlackNumberDao   dao ;
       private   myListener    listener ;
       private   BlackSmsReceiver   blackSmsReceiver   ;
      
      
       /**
       * 代码创建短信到来的广播接受者
       *   @author   Administrator
       *
       */
       public   class   BlackSmsReceiver    extends   BroadcastReceiver{

               @Override
               public   void   onReceive(Context context, Intent intent) {
               
                     //通过指定意图获取短信
                  Object[] objs = (Object[]) intent.getExtras().get(   "pdus" );      //获取短信
               for   (Object obj : objs) {
                  SmsMessage message = SmsMessage. createFromPdu(( byte []) obj);
                  String sender = message.getOriginatingAddress();
                  String body = message.getMessageBody();

                          //判断号码是否在黑名单的短信拦截中
                  String mode =   dao .findMode(sender);
                    if (mode.equals(   "2" )||mode.equals(   "3" )){
                   
                    Log. i( TAG , "发现黑名单短信,拦截"   );
                                abortBroadcast();
                  }
                  
                  
            }
            
      }
      }
      
      
      
      
      
       @Override
       public   IBinder onBind(Intent intent) {
            
               return   null ;
      }
      
       /**
       * 服务创建时
       */
       @Override
       public   void   onCreate() {
            Log. i( TAG ,   "服务创建时"   );
            
               //当服务开启时注册广播意图过滤器,开启拦截短信广播
               blackSmsReceiver =   new   BlackSmsReceiver();
            
            IntentFilter  filter=   new   IntentFilter();     //指定广播事件的意图过滤器
            filter.addAction(   "android.provider.Telephony.SMS_RECEIVED"   );
            filter.setPriority(1000);
            registerReceiver(   blackSmsReceiver , filter);
            
            
            
               dao =   new   BlackNumberDao(   this );
               //1.创建电话管理者
               tm =(TelephonyManager) getSystemService(   TELEPHONY_SERVICE );
            
               listener =   new   myListener();
               //2.给电话管理者设置来电监听事件
               tm .listen(   listener , PhoneStateListener.   LISTEN_CALL_STATE );     //开启监听
            
            
               super .onCreate();
      }
      
       /**
       * 服务销毁时
       */
       @Override
       public   void   onDestroy() {
            Log. i( TAG ,   "服务销毁时"   );
            
               //服务销毁时注销短信拦截广播
            unregisterReceiver(   blackSmsReceiver );
               blackSmsReceiver =   null ;
            
               //服务销毁时关闭电话状态监听
               tm .listen(   listener , PhoneStateListener.   LISTEN_NONE );     //关闭监听
               listener =   null ;
               super .onDestroy();
      }
      
      
       /**
       * 创建来电监听器
       *   @author   Administrator
       *
       */
       private   class   myListener   extends   PhoneStateListener{
            
               private   static   final   String   TAG   =   "myListener" ;

               //当来电状态发生改变时
               @Override
               public   void   onCallStateChanged(   int   state, String incomingNumber) {
                     super .onCallStateChanged(state, incomingNumber);
                  
                     switch   (state) {
                     case   TelephonyManager.   CALL_STATE_RINGING   //来电时
                           //查询来电号码是否在黑名单数据库中
                        String mode=   dao .findMode(incomingNumber);
                           if (mode.equals(   "1" )||mode.equals(   "3" )){
                              Log. i( TAG ,   "拦截黑名单电话"   );
                              endCall();   //调用拦截电话的方法
                              
                                 /*Log.i(TAG, "删除呼叫记录");
                              deleteCallLog(incomingNumber);  //删除呼叫记录*/
                              
                              
                                 // 挂断电话 呼叫记录不是立刻产生出来的.
                                 // deleteCallLog(incomingNumber);
                                 // 在呼叫记录产生之后, 才能去移除呼叫记录.

                                 // 注册一个内容观察者 观察呼叫记录的变化.
                              getContentResolver().registerContentObserver(
                                          Uri. parse( "content://call_log/calls" ),   true ,
                                             new   MyObserver(   new   Handler(), incomingNumber));

                          }
                              
                              
                        
                        
                           break ;
                        
                  case   TelephonyManager.   CALL_STATE_IDLE :      //空闲时候 
                        
                           break ;      

                  
                  }
                  
            }

            
}
      
       /**
       * 挂断电话的方法: 利用发射技术找到系统隐藏的挂断电话的API
       */
       public   void   endCall(){
            
               try   {
                   Class clazz = getClassLoader().loadClass( "android.os.ServiceManager"   );
                  Method method = clazz.getDeclaredMethod( "getService" , new   Class[] { String.   class   });
                  IBinder b = (IBinder) method.invoke(   null ,   new   Object[] {   TELEPHONY_SERVICE   });
                  
                     //调用导入系统文件 aidl中的方法
                  ITelephony iTelephony = ITelephony.Stub.asInterface(b);
                  iTelephony.endCall();

            }   catch   (Exception e) {
                  e.printStackTrace();
            }
            
            
            
      }
      
      
       /**
       * 创建内容观察者
       */
      
       public   class   MyObserver    extends   ContentObserver{

               private   String   incomingNumber ;     //来电号码
            
               public   MyObserver(Handler handler,String incomingNumber) {
                     super (handler);
                  
                     this .   incomingNumber   = incomingNumber;
            
            }
            
               //当数据发生变化的时候
               @Override
               public   void   onChange(   boolean   selfChange) {
                     super .onChange(selfChange);
                  getContentResolver().unregisterContentObserver(   this );
                  Log. i( TAG , "观察到数据库内容变化.删除呼叫记录"   );
                  deleteCallLog(   incomingNumber );
                  
            }
            
            
            
      }
      
      
      
      
      
            /**
           * 删除呼叫记录
           *   @param   incomingNumber
           */
               public   void   deleteCallLog(String incomingNumber) {
                  ContentResolver resoler = getContentResolver();
                  resoler.delete(Uri. parse( "content://call_log/calls" ),   "number=?" , new   String[] { incomingNumber });
            }
      
      

}





 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值