从 android 的收件箱中删除收到的邮件
delete incoming message from inbox in android
我想从收件箱中删除收到的消息,只想在我的应用程序中接收
我正在尝试这段代码
但它不起作用,我正在使用棒棒糖
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from = null;
String msgBody = null;
if (bundle != null){
//---retrieve the SMS message received---
try{
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]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
}
}catch(Exception e){
Log.d("Exception caught", e.getMessage());
}
Toast.makeText(context, "Number : " +msg_from + "\n" + "Message : "+ msgBody , Toast.LENGTH_LONG).show();
clearAbortBroadcast();
this.abortBroadcast();
}
}
}
}
首先,确保在清单文件中为您的接收器设置尽可能高的优先级。
现在,在 KitKat 和更新的 Android 版本上,您还必须确保您的应用程序被选为默认短信应用程序并且正在收听 SMS_DELIVER_ACTION
。不是 SMS_RECEIVED
。否则这个 won't work:
On Android 4.4, only one app can receive the new SMS_DELIVER_ACTION
intent, which the system broadcasts when a new SMS message arrives.
(...) only the app that receives the SMS_DELIVER_ACTION broadcast (the
user-specified default SMS app) is able to write to the SMS Provider.
我想从收件箱中删除收到的消息,只想在我的应用程序中接收 我正在尝试这段代码 但它不起作用,我正在使用棒棒糖
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from = null;
String msgBody = null;
if (bundle != null){
//---retrieve the SMS message received---
try{
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]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
}
}catch(Exception e){
Log.d("Exception caught", e.getMessage());
}
Toast.makeText(context, "Number : " +msg_from + "\n" + "Message : "+ msgBody , Toast.LENGTH_LONG).show();
clearAbortBroadcast();
this.abortBroadcast();
}
}
}
}
首先,确保在清单文件中为您的接收器设置尽可能高的优先级。
现在,在 KitKat 和更新的 Android 版本上,您还必须确保您的应用程序被选为默认短信应用程序并且正在收听 SMS_DELIVER_ACTION
。不是 SMS_RECEIVED
。否则这个 won't work:
On Android 4.4, only one app can receive the new SMS_DELIVER_ACTION intent, which the system broadcasts when a new SMS message arrives. (...) only the app that receives the SMS_DELIVER_ACTION broadcast (the user-specified default SMS app) is able to write to the SMS Provider.