我想在发送之前读取用户在我的应用程序中发送的短信。有没有办法实现这个目标?
答案 0 :(得分:2)
这是不可能的。任何应用程序都可以使用SmsManager
发送短信,并且不能拦截此类消息,除非是自定义固件。
答案 1 :(得分:0)
你无法阻止传出的短信。你可以在发送之后找到仅。你可以通过注册sms的内容观察者来实现,当短信来到发送框时。
答案 2 :(得分:0)
如果需要,您可以截取传入的消息。
以下是一个SMS拦截器示例,如果它包含一些自定义数据,则“取消”SMS:
要使您的应用程序在手机中显示消息之前接收消息,您必须在清单中定义具有高优先级的接收方。例如:
<receiver android:name=".SMSReceiver">
<intent-filter android:priority="9999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
然后,创建接收器:
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 SMSReceiver extends BroadcastReceiver{
private static final String CRITICAL_MESSAGE = "critical";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null){
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if (msgs[i].getMessageBody().toString().equals(CRITICAL_MESSAGE)){
str = "Critical msg from " + msgs[i].getOriginatingAddress() + " !";
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
abortBroadcast();
}
}
}
}
}
如果收到 critical 字符串,上述接收方会取消短信(中止广播)。