短信 android
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();
}
} ide
} spa
}
} xml
对应的AndroidMainfest.xml 对象
<receiver
电话 get
public class TelephoneIntercept extends BroadcastReceiver {
private final String INCOMING_NUMBER = "incoming_number"; it
@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;
} io
}
} table
对应的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" />