如果没有拦截的话 也不要慌 对于有些手机是没有用的,所以这种去了解玩玩就可以了
直接上代码:
xml权限配置
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.zking.laci.android22_broacast">
- <!--接收广播的权限-->
- <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
- <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true" android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver android:name=".MySMSReceiver">
- <intent-filter android:priority="1000"
- >
- <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
- </intent-filter>
- </receiver>
- </application>
- </manifest>
ava代码:记得新创个类
- package com.zking.laci.android22_broacast;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.telephony.SmsMessage;
- import android.util.Log;
- /**
- * Created by Laci on 2017/7/13.
- */
- public class MySMSReceiver extends BroadcastReceiver{
- private String a;
- @Override
- public void onReceive(Context context, Intent intent) {
- if("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())){
- Log.i("test","来了");
- //得到短信的内容
- Bundle bundle=intent.getExtras();
- Object[] o= (Object[]) bundle.get("pdus");
- //实例化SmsMessage
- SmsMessage smsMessage[]=new SmsMessage[o.length];
- //把o里面的数据传递给smsMessage
- for (int i = 0; i < o.length; i++) {
- smsMessage[i]=SmsMessage.createFromPdu((byte[]) o[i]);
- }
- for (SmsMessage message : smsMessage) {
- a = message.getDisplayOriginatingAddress();
- Log.i("test","短信发送者"+message.getDisplayOriginatingAddress()+":"+message.getDisplayMessageBody());
- }
- //如果有例如下方的电话时就直接拦截,如果没有拦截的话 也不要慌 对于有些手机是没有用的,所以这种去玩玩就可以了
- if("+86187xxxxxxxxxx4".equals(a)){
- abortBroadcast();
- //内容提供者
- //删除系统收短信的数据库表
- Log.i("test","拦截不了");
- }
- }
- }
- }