阻止Android上的传入/传出短信

时间:2011-02-09 10:54:15

标签: android sms block

有没有人知道通过代码阻止传入/传出SMS消息的可靠方法?如果正在接收实际的SMS消息,这是可以的,但我想阻止接收该SMS的任何通知。此外,不应允许用户发送(或者甚至可以输入)SMS消息。这可能吗?

由于

2 个答案:

答案 0 :(得分:3)

您无法阻止外发短信。

这是我用来阻止传入文本的内容。


SmsReceiver.java

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 **********************");
        }

    }

}

}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="APP.PACKAGE.NAMEHERE"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<supports-screens 
android:largeScreens="true" 
android:normalScreens="true" 
android:smallScreens="true" 
android:resizeable="true" 
android:anyDensity="true" />

<uses-feature android:name="android.hardware.telephony" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".APPACTIVITYHERE"
        android:label="@string/app_name"
        android:configChanges="orientation|keyboardHidden" >


    <service android:name=".MyService" android:enabled="true"/>
     <receiver android:name="SmsReceiver">
      <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="SmsReceiver">
        <intent-filter android:priority="2147483645">
         <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
      </receiver>

</application>

从清单中拿走的主要内容是服务块,接收器块和权限。

答案 1 :(得分:2)

关于阻止传入的短信。您可以注册具有高优先级的广播接收器,以便在调用系统应用程序之前调用您的代码。与广播接收器相比,您可以中止广播,而其他应用则无法看到它。

在清单中注册接收者:

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

然后在广播接收器中:

@Override
public void onReceive(Context context, Intent intent) {
    abortBroadcast();
}