在Android的收件箱中删除传入的消息 [英] delete incoming message from inbox in android
问题描述
我想从收件箱中删除传入的消息,只是想获得我的应用程序
我试图此code
但它不工作,我用棒棒糖
公共类SmsReceiver扩展广播接收器{\r
\r
@覆盖\r
公共无效的onReceive(上下文的背景下,意图意图){\r
如果(intent.getAction()。等于(android.provider.Telephony.SMS_RECEIVED)){\r
捆绑包= intent.getExtras(); // ---获得通过SMS消息---\r
SmsMessage []封邮件= NULL;\r
字符串msg_from = NULL;\r
字符串msgBody = NULL;\r
如果(捆绑!= NULL){\r
// ---检索收到的短信---\r
尝试{\r
[对象]的PDU =(对象[])bundle.get(的PDU);\r
封邮件=新SmsMessage [pdus.length]\r
的for(int i = 0; I< msgs.length;我++){\r
封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);\r
msg_from =封邮件[I] .getOriginatingAddress();\r
msgBody =封邮件[I] .getMessageBody();\r
}\r
}赶上(例外五){\r
Log.d(捕获到异常,e.getMessage());\r
}\r
\r
Toast.makeText(背景下,数字+ msg_from +\\ n+留言:+ msgBody,Toast.LENGTH_LONG).show();\r
\r
clearAbortBroadcast();\r
this.abortBroadcast();\r
\r
}\r
\r
}\r
}\r
\r
}
\r
首先,确保优先级最高的可能设置为接收器的清单文件。
现在,在奇巧和更新的Android版本,你还必须确保你的应用程序被选定为默认的短信应用,并听取了 SMS_DELIVER_ACTION
。没有 SMS_RECEIVED
。否则,此将无法正常工作
在Android 4.4系统中,只有一个应用程序可以接收新的SMS_DELIVER_ACTION
意图,当一个新的SMS消息到达该系统广播。
(...)只接收SMS_DELIVER_ACTION广播(该应用程序
用户指定的默认短信应用)能够写短信提供商。
块引用>i want to delete incoming message from the inbox and just want to receive in my app i am trying this code but it is not working, i am using lollipop
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(); } } } }
解决方案First, make sure to set the highest priority possible to your receiver on the manifest file.
Now, on KitKat and newer Android versions, you also have to make sure that your app is selected as the default SMS app and is listening to the
SMS_DELIVER_ACTION
. NotSMS_RECEIVED
. Otherwise this 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.
这篇关于在Android的收件箱中删除传入的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!