【发布时间】:2016-05-07 06:21:43
【问题描述】:
我试图阻止我的 android 设备中的所有传入短信。
这是我使用的代码-
public class SmsReceiver 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;
@Override
public void onReceive(Context context, Intent intent) {
String MSG_TYPE = intent.getAction();
if (MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
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++) {
Log.i("log", "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++) {
Log.i("log", "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++) {
Log.i("log", "Blocking SMS **********************");
}
}
}
}
清单文件-
<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>
它显示我阻止了短信,但又收到了短信。所以这段代码对我不起作用。我在关注this SO Question.
我正在查看的其他问题是 -
Android Block Incoming SMS using BroadCastReceiver
有人对此有什么建议吗?
【问题讨论】:
-
所以谁能告诉我为什么我得到这个-1。 ?