从收件箱中删除短信
我想在用户阅读短信后将其从收件箱中删除。如何做到这一点?
编辑:
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String address = null;
if(bundle!=null) {
String info = " ";
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]);
address=msgs[i].getDisplayOriginatingAddress();
info += msgs[i].getMessageBody().toString();
}
/*String str=bundle.getString("state");
Log.v("State",str);*/
if((PhoneNumberUtils.isWellFormedSmsAddress(address))){ //set ! and address length
//abortBroadcast();
Log.v("phone num","wellformed");
Uri deleteUri = Uri.parse("content://sms");
Cursor c = context.getContentResolver().query(deleteUri, null, null,
null, null);
while (c.moveToNext()) {
try {
// Delete the SMS
String pid = c.getString(0); // Get id;
String uri = "content://sms/conversations/" + pid;
context.getContentResolver().delete(Uri.parse(uri),
null, null);
} catch (Exception e) {
Log.v("exception","occurred");
}
}
}
}
}
}
这段代码有什么问题?短信没有被删除
I would like to delete an SMS from the inbox once it is read by the user. How to do this?
Edit:
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String address = null;
if(bundle!=null) {
String info = " ";
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]);
address=msgs[i].getDisplayOriginatingAddress();
info += msgs[i].getMessageBody().toString();
}
/*String str=bundle.getString("state");
Log.v("State",str);*/
if((PhoneNumberUtils.isWellFormedSmsAddress(address))){ //set ! and address length
//abortBroadcast();
Log.v("phone num","wellformed");
Uri deleteUri = Uri.parse("content://sms");
Cursor c = context.getContentResolver().query(deleteUri, null, null,
null, null);
while (c.moveToNext()) {
try {
// Delete the SMS
String pid = c.getString(0); // Get id;
String uri = "content://sms/conversations/" + pid;
context.getContentResolver().delete(Uri.parse(uri),
null, null);
} catch (Exception e) {
Log.v("exception","occurred");
}
}
}
}
}
}
What is wrong with this code? The sms is not getting deleted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下方法从收件箱中删除短信,
You can use following method for deleting SMS from Inbox,
尝试这个完整的解决方案删除...
try this for complete solution for deleting...