//拦截短信验证码的广播
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();//拦截短信
}
}
}
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.
迷茫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