短信 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" />