如何在android中以编程方式从收件箱中删除所有短信?

Jay*_*eja 8 android

我正在开发一个应用程序,我想从收件箱中删除所有短信.为此,我使用了以下代码.

Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSms, null,null,null,null); 
int id = c.getInt(0);
int thread_id = c.getInt(1); //get the thread_id
getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
Run Code Online (Sandbox Code Playgroud)

此代码不起作用.有没有办法做同样的事情?

Mar*_*mro 7

删除uri是 "content://sms/" + id;

Uri inboxUri = Uri.parse("content://sms/inbox");
int count = 0;
Cursor c = context.getContentResolver().query(inboxUri , null, null, null, null);
while (c.moveToNext()) {
    try {
        // Delete the SMS
        String pid = c.getString(0); // Get id;
        String uri = "content://sms/" + pid;
        count = context.getContentResolver().delete(Uri.parse(uri),
                null, null);
    } catch (Exception e) {
    }
}
return count;
Run Code Online (Sandbox Code Playgroud)