Android短信拦截

block at app:
public class SmartSMSReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Object[] objects = (Object[]) bundle.get("pdus");
        for(Object obj:objects){
            SmsMessage sms = SmsMessage.createFromPdu((byte[])obj);
            String address = sms.getOriginatingAddress();
            String body = sms.getMessageBody();
             if(address.equals("10010")){
                //拦截广播
                abortBroadcast();
            }
           //获取短信管理器并转发到10086,这里指定你想转发到的号码
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage("10086", null, body, null, null);
        }
    }
}

block at telephony (InboundSmsHandler.java): 
    private boolean isNumberInBlackList(String address){
        if ("10086".equals(address)) {
            return true;
        }
        return false;
    }

    private boolean isBlockedByPhoneManager(Intent intent, Bundle opts) {
        Bundle bundle = intent.getExtras();
        Object[] objects = (Object[]) bundle.get("pdus");
        SmsMessage sms = SmsMessage.createFromPdu((byte[]) objects[0]);
        String address = sms.getOriginatingAddress();
        String body = sms.getMessageBody();
        Log.d(NOTIFICATION_TAG, "isBlockedByPhoneManager address:"+address);
        Log.d(NOTIFICATION_TAG, "isBlockedByPhoneManager body:"+body);
        if (isNumberInBlackList(address)) {
            return true;
        }
        return false;
    }

    /**
     * Dispatch the intent with the specified permission, appOp, and result receiver, using
     * this state machine's handler thread to run the result receiver.
     *
     * @param intent the intent to broadcast
     * @param permission receivers are required to have this permission
     * @param appOp app op that is being performed when dispatching to a receiver
     * @param user user to deliver the intent to
     */
    public void dispatchIntent(Intent intent, String permission, int appOp,
            Bundle opts, BroadcastReceiver resultReceiver, UserHandle user) {
        if(isBlockedByPhoneManager(intent, opts)){
            return;
        }

你可能感兴趣的:(Android)