短信,电话拦截

短信

public class SMSIntercept extends BroadcastReceiver {
 private final String PDUS = "pdus";

 @Override
 public void onReceive(Context context, Intent intent) {
  Bundle bundle = intent.getExtras();
  if (bundle != null) {
   // 获取收到的短息数据
   Object[] objArray = (Object[]) bundle.get(PDUS);
   // 定义封装短信内容的SmsMessage对象的数组
   SmsMessage[] messages = new SmsMessage[objArray.length];
   // 循环处理收到的所有短信内容
   for (int i = 0; i < objArray.length; i++) {
    // 将每条短信数据转化成SmsMessage对象
    messages[i] = SmsMessage.createFromPdu((byte[]) objArray[i]);
    // 获取发送短信的手机号码
    String telPhoneNum = messages[i].getOriginatingAddress();
    // 短信内容
    String displayMessageBody = messages[i].getDisplayMessageBody();
    Hashtable<String, String> messageInterceptHT = DataCache.getInstance().getMessageInterceptHT();
    // messageInterceptHT中含有被屏蔽号码,则停止广播继续传递,进行短信拦截
    if (messageInterceptHT.containsKey(telPhoneNum)) {
     abortBroadcast();
    }
   }

  }

 }
}

对应的AndroidMainfest.xml

<receiver
            android:name=".broadcast.SMSIntercept"
            android:enabled="true" >
            <intent-filter android:priority="700" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
 </receiver>

 

电话

public class TelephoneIntercept extends BroadcastReceiver {
 private final String INCOMING_NUMBER = "incoming_number";

 @Override
 public void onReceive(Context context, Intent intent) {
  // 获取电话管理服务,以便获得电话状态
  TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  Hashtable<String, String> telephoneInterceptHT = DataCache.getInstance().getTelephoneInterceptHT();
  switch (tm.getCallState()) {
  case TelephonyManager.CALL_STATE_RINGING:// 响铃
   String incomingNumber = intent.getStringExtra(INCOMING_NUMBER);
   if (telephoneInterceptHT.containsKey(incomingNumber)) {
    // 采取反射技术访问SDK内部功能来挂断电话
    Class<TelephonyManager> telephonyManagerClass = TelephonyManager.class;
    // 通过反射获取getITelephony方法对应的Method对象
    try {
     Method telephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony", (Class[]) null);
     // 允许访问getITelephony方法
     telephonyMethod.setAccessible(true);
     // 调用getITelephony方法获取ITelephony对象
     Object obj = telephonyMethod.invoke(tm, (Object[]) null);
     // 获取endCall方法对应的Method对象
     Method endCallMethod = obj.getClass().getMethod("endCall", null);
     // 允许访问endCall方法
     endCallMethod.setAccessible(true);
     // 调用endCall方法挂断电话
     endCallMethod.invoke(obj, null);
    } catch (SecurityException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (NoSuchMethodException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (IllegalArgumentException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (IllegalAccessException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (InvocationTargetException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   break;
  case TelephonyManager.CALL_STATE_OFFHOOK:// 接听电话
   break;
  case TelephonyManager.CALL_STATE_IDLE:// 挂断电话
   break;
  }

 }
}

对应的AndroidMainfest.xml

 <receiver
            android:name=".broadcast.TelephoneIntercept"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver> 

<!-- 监听电话状态 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

 

你可能感兴趣的:(短信,电话拦截)