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

阻止Android上的传入/传出短信,android,sms,block,Android,Sms,Block,是否有人知道一种可靠的方法可以通过代码阻止传入/传出SMS消息?如果收到的是实际的短信,这没关系,但我想阻止收到任何短信通知。此外,用户不应被允许发送(或键入)SMS消息。可能吗 感谢您阻止传入的sms。您可以注册具有高优先级的广播接收器,以便在调用系统应用程序之前调用您的代码。比在广播接收器中,您可以中止广播,其他应用程序将看不到它 在舱单中注册接收人: <receiver android:name=".SmsReceiver"> <intent-filter and

是否有人知道一种可靠的方法可以通过代码阻止传入/传出SMS消息?如果收到的是实际的短信,这没关系,但我想阻止收到任何短信通知。此外,用户不应被允许发送(或键入)SMS消息。可能吗


感谢您阻止传入的sms。您可以注册具有高优先级的广播接收器,以便在调用系统应用程序之前调用您的代码。比在广播接收器中,您可以中止广播,其他应用程序将看不到它

在舱单中注册接收人:

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

您无法阻止传出的文本消息

下面是我用来阻止传入文本的方法


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

    }

}

}
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.sms消息;
导入android.widget.Toast;
公共类BroadCastReceiver扩展了BroadCastReceiver
{
/**在首次创建活动时调用*/
私有静态最终字符串ACTION=“android.provider.Telephony.SEND_SMS”;
公共静态int MSG_TPE=0;
公共void onReceive(上下文、意图)
{ 
字符串MSG_TYPE=intent.getAction();
if(MSG_TYPE.equals(“android.provider.Telephony.SMS_RECEIVED”))
{
//Toast Toast=Toast.makeText(上下文,“收到短信:+MSG\u TYPE,Toast.LENGTH\u LONG);
//toast.show();
Bundle=intent.getExtras();
对象消息[]=(对象[])bundle.get(“PDU”);
SmsMessage SmsMessage[]=新SmsMessage[messages.length];
对于(int n=0;n对于(int i=0;我再次检查。这让我发疯。我已经取得了阻止通过广播接收器接收传入短信通知的进展,但阻止传出短信让我发疯……理想情况下,我甚至不希望用户打开短信应用程序。有没有办法杀死外部进程(即短信应用程序)?我已经通过restartPackage尝试过了,但它意外地终止了我的应用程序。我想这是因为权限/安全问题?哦,对于那些想知道我为什么需要这样做的人来说,这是因为我需要创建一个类似于Textecution(www.Textecution.com)的应用程序.谢谢!@Vishnu Mohan G:最高优先级为1000,注册一个优先级为1000的接收器。然后在onReceive方法中,这个.abortBroadcast()将中止广播。Android文档说“该值必须是整数,例如”100“。数字越大优先级越高。默认值为0。该值必须大于-1000且小于1000”,因此根据语句,该值必须为999。
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 **********************");
        }

    }

}

}
<?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>