在android中阻止外发短信/彩信

时间:2011-11-16 02:15:01

标签: android

我在进入收件箱之前已阅读传入短信内容并阻止相同内容。代码如下:

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并阻止SMS输入收件箱。 但我的问题是,相同的代码不适用于外发短信。它不会阻止外发短信。我在AndroidManifest中注册了BroadcastReceiver,如下所示。

<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"/>

任何人都可以帮助我出错了,为什么我无法阻止外出短信。感谢

2 个答案:

答案 0 :(得分:2)

仅在触发操作android.provider.Telephony.SMS_RECEIVED时调用您的接收器。因此,当发送SMS时,它不会做出反应。

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

答案 1 :(得分:0)

我在发送短信时捕获了这个事件,我在“content:// sms /”上使用观察者,我进行查询,然后我获取最后发送的短信然后我将其删除:getContentResolver()。delete(“content” :// sms /“,”_ id =?“,new String [] {m​​essage_id});

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