【问题标题】:Block incoming SMS阻止传入的短信
【发布时间】:2016-05-07 06:21:43
【问题描述】:

我试图阻止我的 android 设备中的所有传入短信。

这是我使用的代码-

public class SmsReceiver extends BroadcastReceiver {
/**
 * Called when the activity is first created.
 */
private static final String ACTION = "android.provider.Telephony.SEND_SMS";
public static int MSG_TPE = 0;

@Override
public void onReceive(Context context, Intent intent) {
    String MSG_TYPE = intent.getAction();
    if (MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {

        Bundle bundle = intent.getExtras();
        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[messages.length];
        for (int n = 0; n < messages.length; n++) {
            smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }

        // show first message
        Toast toast = Toast.makeText(context, "BLOCKED Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
        toast.show();
        abortBroadcast();
        for (int i = 0; i < 8; i++) {
            Log.i("log", "Blocking SMS **********************");
        }

    } else if (MSG_TYPE.equals("android.provider.Telephony.SEND_SMS")) {
        Toast toast = Toast.makeText(context, "SMS SENT: " + MSG_TYPE, Toast.LENGTH_LONG);
        toast.show();
        abortBroadcast();
        for (int i = 0; i < 8; i++) {
            Log.i("log", "Blocking SMS **********************");
        }

    } else {

        Toast toast = Toast.makeText(context, "SIN ELSE: " + MSG_TYPE, Toast.LENGTH_LONG);
        toast.show();
        abortBroadcast();
        for (int i = 0; i < 8; i++) {
            Log.i("log", "Blocking SMS **********************");
        }

    }

}

}

清单文件-

    <service android:name=".MyServiceSentReceived" android:enabled="true"/>

    <receiver android:name="SmsReceiver">
        <intent-filter android:priority="2147483645">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>

它显示我阻止了短信,但又收到了短信。所以这段代码对我不起作用。我在关注this SO Question.

我正在查看的其他问题是 -

Block SMS in android

Android Block Incoming SMS using BroadCastReceiver

有人对此有什么建议吗?

【问题讨论】:

  • 所以谁能告诉我为什么我得到这个-1。 ?

标签: java android


【解决方案1】:

自 API 19 (KitKat) 起,您无法阻止传入的 SMS,除非您的应用是默认消息传递应用,即便如此,您也只能阻止它们被保存到 SMS Provider。

SMS_RECEIVED 广播不能再被中止,所以任何监听它的应用程序仍然会得到它。此外,默认应用无论如何都会收到不同的广播 - SMS_DELIVER - 任何其他应用都不会收到,也无法被拦截。

如果您的应用是默认 SMS 应用,它负责将传入的消息写入 SMS Provider,因此如果您不希望它们保存在那里,就不要编写它们。但是,这不会对SMS_RECEIVED 广播产生影响,它仍将传送到为其注册的任何应用程序,尽管这些应用程序将无法将它们写入提供程序。

以下博客页面讨论了 SMS API 更改,并详细介绍了作为默认消息传递应用程序的应用程序的要求。但请注意,默认应用负责很多很多事情 - 包括彩信,以及 - 编写一个成熟的消息传递客户端并非易事。

Getting Your SMS Apps Ready for KitKat

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多