我正在编写一个应用程序,它需要中断短信传入事件然后修改短信体。用户必须获得修改后的信息,而非原创信使用BroadcastReceiver中断短信事件太简单了,这是我的代码:
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//this.abortBroadcast(); // stop notification to user
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String sender = "";
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus"); // PDU - "protocol description unit", which is the industry format for an SMS message
msgs = new SmsMessage[pdus.length];
// large message might be broken into many
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
sender = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
System.out.println("Msg Body: " + msgBody + " From: " + sender);
/// I need to modify msgBody
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
但我无法改变消息体。有没有办法做到这一点? 如果没有,有没有办法在我自己的收件箱中创建这个短信并将通知发送给用户?
答案 0 :(得分:0)
我想你需要的是注册一个具有最高优先级的接收器,所以当你的接收器拿起传入的短信执行abortBroadcast()时,这将确保其他应用程序在线处理不会收到它来破坏你的更改。 / p>
进一步说,您可以使用最新收到的短信查询SMS数据库查找表中此条目的row_id,就是这样,它现在被修剪为更新查询以修改文本。
在少数情况下,这可能需要系统应用程序权限,在这种情况下,您的设备需要植根。