search

Home  >  Q&A  >  body text

处理Android中,设置应用的优先级最高,然而短信应用依然最先收到广播导致无法拦截短信的问题

//拦截短信验证码的广播

    IntentFilter intercptFilter = new IntentFilter();
    intercptFilter.addAction(SmsInterceptReceiver.ACTION_SMS_INTERCEPT);
    intercptFilter.setPriority(Integer.MAX_VALUE);
    smsInterceptReceiver = new SmsInterceptReceiver();
    mContext.registerReceiver(smsInterceptReceiver, intercptFilter);

//发送广播
context.sendOrderedBroadcast(intent, null);

@Override

public void onReceive(Context context, Intent intent) {
    // 拦截短信
    String action = intent.getAction();
    Log.d("打印广播类型:"+action);
    Bundle bundle = intent.getExtras();
    if(bundle==null){
        return;
    }

    if(action!=null&&action.equals(ACTION_SMS_INTERCEPT)){
        Object[] pdus = (Object[])bundle.get("pdus");

        if (pdus != null && pdus.length > 0)
        {
            SmsMessage[] messages = new SmsMessage[pdus.length];
            int length = messages.length;
            for (int i = 0; i < length; i++)
            {
                byte[] pdu = (byte[])pdus[i];
                messages[i] = SmsMessage.createFromPdu(pdu);
            }
            for (SmsMessage msg : messages){
                 // 获取短信内容
                String content = msg.getMessageBody();
                String sender = msg.getOriginatingAddress();
                analysisMessage(sender,content);
            }
            Log.d("是否要向用户显示短信,1是:"+isShowSms);
            if(isShowSms==1){
                abortBroadcast();//拦截短信
            }
        }
    }
巴扎黑巴扎黑2772 days ago502

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:36:25

    There is no problem with your code, but the function you want to implement is not very good. Intercepting phone text messages is not possible on most mobile phones today. After all, phone text messages are the most basic use of mobile phones. It is impossible to install an APP and have this function affected. This code was feasible in early Android versions, but now Android has greatly restricted this behavior of intercepting system broadcasts.

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:36:25

    After Android 4.4, if your application is not the system’s SMS application, you will not be able to grab the broadcast. SMS interception is not a good thing

    reply
    0
  • Cancelreply