Android 4.4版本后无法拦截短信问题

在4.4版本后使用 this.abortBroadcast(); 中断短信广播,发现还是能收到短信,原因是谷歌设立了安全机制,只有默认短信应用才能操作,否则没有权限,就没法玩了。

下面直接上代码很简单

1:AndroidManifest.xml中

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER"
            />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <action android:name="android.intent.action.SENDTO" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="sms" />
        <data android:scheme="smsto" />
        <data android:scheme="mms" />
        <data android:scheme="mmsto" />
    </intent-filter>
</activity>

2:创建工具类 ComposeSmsActivity、HeadlessSmsSendService、MmsReceiver这三个是必需的,里面可以不做任何操作

我这里写了一个短信接收广播SmsReceiver(作用是不接收信息,并删除该条信息)

import android.app.Activity;
import android.os.Bundle;

public class ComposeSmsActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
}

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class HeadlessSmsSendService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    }
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;


public class SmsReceiver extends BroadcastReceiver {

    public SmsReceiver() {
        Log.i("cky", "new SmsReceiver");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        abortBroadcast();
        long id = getThreadId(context);
        Uri mUri = Uri.parse("content://sms/conversations/" + id);
        context.getContentResolver().delete(mUri, null, null);
    }

    private long getThreadId(Context mContext) {
        long threadId = 0;
        String SMS_READ_COLUMN = "read";
        String WHERE_CONDITION = SMS_READ_COLUMN + " = 0";
        String SORT_ORDER = "date DESC";
        int count = 0;
        Cursor cursor = mContext.getContentResolver().query(
                Uri.parse("content://sms/inbox"), new String[]{"_id", "thread_id", "address", "person", "date", "body"},
                WHERE_CONDITION, null, SORT_ORDER);
        if (cursor != null) {
            try {
                count = cursor.getCount();
                if (count > 0) {
                    cursor.moveToFirst();
                    threadId = cursor.getLong(1);
                }
            } finally {
                cursor.close();
            }
        }
        Log.e("threadId", String.valueOf(threadId));
        return threadId;
    }

}

3:AndroidManifest.xml添加(包名自行修改)

<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver
    android:name=".message.MmsReceiver"
    android:permission="android.permission.BROADCAST_WAP_PUSH">
    <intent-filter>
        <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
        <data android:mimeType="application/vnd.wap.mms-message" />
    </intent-filter>
</receiver>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<receiver
    android:name=".MyTelReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
</receiver>
<!-- Service that delivers messages from the phone "quick response" -->
<service
    android:name=".message.HeadlessSmsSendService"
    android:exported="true"
    android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
    <intent-filter>
        <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
        <category android:name="android.intent.category.DEFAULT" />

        <data android:scheme="sms" />
        <data android:scheme="smsto" />
        <data android:scheme="mms" />
        <data android:scheme="mmsto" />
    </intent-filter>
</service>

4:在主界面onResume中检查是否为默认短信应用,否的话弹框请用户选择

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onResume() {
    super.onResume();
    checkIfDefaultSMS();
}

/**
 * 检测是否为默认短信应用,否的话弹框请用户选择
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void checkIfDefaultSMS() {
    final String myPackageName = getPackageName();
    if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
        // App is not default.
        // Show the "not currently set as the default SMS app" interface
        View viewGroup = findViewById(R.id.ll_not_default_app);
        viewGroup.setVisibility(View.VISIBLE);
        // Set up a button that allows the user to change the default SMS app
        View button = findViewById(R.id.tv_change_default_app);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName);
                startActivity(intent);
            }
        });
    } else {
        // App is the default.
        // Hide the "not currently set as the default SMS app" interface
        View viewGroup = findViewById(R.id.ll_not_default_app);
        viewGroup.setVisibility(View.GONE);
    }
}

5:附上xml代码

<LinearLayout
    android:id="@+id/ll_not_default_app"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_gravity="center_horizontal"
    android:background="#FFE4C4"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:visibility="visible"
    tools:ignore="MissingConstraints">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用短信功能,需设为默认短信应用。"
        android:textColor="#FF0000" />

    <TextView
        android:id="@+id/tv_change_default_app"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:text="设置"
        android:textColor="#008000" />
</LinearLayout>

6:做完短信数据操作后如果需要恢复短信为默认短信应用(不恢复每次打开短信就会提示你恢复)

//com.android.mms 就是短信的包名

Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, "com.android.mms");

startActivity(intent);

 
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木易明~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值