在安卓手机上阻止收发短信

3

有没有可靠的方法可以通过代码屏蔽进出短信?如果实际的短信消息被接收到,那也没关系,但我希望屏蔽任何收到短信的通知。此外,用户不应该被允许发送(最好甚至不要输入)短信。这是否可能?

谢谢


再检查一遍。这让我疯了。我已经通过BroadcastReceiver阻止了来电短信通知的进展,但是阻止发出的短信让我发疯了...理想情况下,我甚至不希望用户打开消息应用程序。有没有办法杀死外部进程(即消息应用程序)?我已经尝试过restartPackage,但那会意外终止我的应用程序。我认为这是因为权限/安全问题?哦,对于那些想知道我为什么需要这样做的人,这是因为我需要创建一个类似于Textecution(www.textecution.com)的应用程序。谢谢! - Marian Busoi
2个回答

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>

从清单文件中需要重点关注服务块、接收器块以及权限。

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();
}

3
最高优先级为1000,使用1000优先级注册一个接收器。在onReceive方法中,使用this.abortBroadcast()可以中止广播。 - blackfyre
1
Android文档中提到:“该值必须是一个整数,例如“100”。数字越大,优先级越高。默认值为0。该值必须大于-1000且小于1000”,因此根据该语句,它必须是999。http://developer.android.com/guide/topics/manifest/intent-filter-element.html - Syed Raza Mehdi

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