2012-02-25 43 views
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); 

此代碼無效。 有沒有辦法做同樣的事情?

+0

http://stackoverflow.com/questions/419184/how-to-delete-an-sms-from-the-inbox-in-android-以編程方式 – 2012-02-25 11:02:26

回答

6

的刪除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; 
+0

這是爲單個messge或交談? – 2017-06-09 04:07:15

0
//delete all call logs 
Uri callLog = Uri.parse("content://call_log/calls"); 
int rs1 = getContentResolver().delete(callLog, null, null); 

//delete all sms 
Uri inboxUri = Uri.parse("content://sms/");  
int rs2 = getContentResolver().delete(inboxUri, Sms._ID + "!=?", new String[]{"0"}); 
相關問題