在安卓系统中阻止发送短信/彩信

3

我已经阅读了来自短信的内容,并在其进入收件箱之前将其屏蔽。以下是代码:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class BroadCastReceiver 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;
    public void onReceive(Context context, Intent intent) 
    { 
        String MSG_TYPE=intent.getAction();
            if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED"))
        {
//          Toast toast = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
//          toast.show();

        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++)
            {
                System.out.println("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++)
            {
                System.out.println("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++)
            {
                System.out.println("Blocking SMS **********************");
            }

        }

    }

}

该代码在收到短信时运行良好。Toast显示Sms pdu,阻止短信进入收件箱。 但我的问题是同样的代码对于发出的短信不起作用。它无法阻止发出的短信。我已在AndroidManifest中注册了广播接收器,如下所示。

<service  android:name=".MyService"    android:enabled="true"/>
         <receiver android:name="BroadCastReceiver">
                <intent-filter  android:priority="2147483647">
                    <action   android:name="android.provider.Telephony.SMS_SENT"/>
                </intent-filter>
         </receiver>
    <service  android:name=".MyServiceSentReceived"    android:enabled="true"/>
             <receiver android:name="BroadCastReceiver">
                    <intent-filter  android:priority="2147483645">
                        <action   android:name="android.provider.Telephony.SMS_RECEIVED"/>
                    </intent-filter>
             </receiver>
            <intent-filter>
                <action android:name="android.intent.action.SENDTO"></action>
                <data android:scheme="smsto"></data>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>

    and Permissions Like:
    <uses-permission android:name="android.permission.RECEIVE_SMS" />  
         <uses-permission android:name="android.permission.READ_SMS"/>
         <uses-permission android:name="android.permission.SEND_SMS"/>

请问有谁能帮忙解决问题,我为什么不能阻止发送短信呢?感谢。


请查看我的答案:(http://www.stackoverflow.com/a/24996424/3884250)!我在这里解释了如何阻止出站和入站短信。 - Nadeem Iqbal
2个回答

2

只有当操作 android.provider.Telephony.SMS_RECEIVED 被触发时,您的接收器才会被调用。因此,它不会在短信发送时做出反应。

我远非专家,但我认为没有可能阻止发出的短信。默认的短信应用使用 android.telephony.SmsManager 的方法 SendDataMessage() ,它不发送任何广播。


0

我在发送短信时捕获了事件,我在“content://sms/”上使用观察器,进行查询并获取最后一条发送的短信,然后将其删除:getContentResolver().delete("content://sms/","_id=?",new String[] { message_id});

我的问题是这个想法在模拟器上完美运行,但在真实设备上却不行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接