2012-11-05 117 views
2

我在內容解析器的幫助下訪問所有收件箱中的郵件但現在問題是我想從收件箱中刪除多個郵件或單個郵件。我發現所有消息的刪除功能不是針對單個消息或多個消息。我將所有消息存儲在ArrayList中。 任何幫助將不勝感激。如何從Android的收件箱中刪除多條消息或單條消息?

我讀消息的代碼是: -

Uri uriSMSURI = Uri.parse("content://sms/inbox"); 
Cursor cur = getContentResolver().query(uriSMSURI , null, null, null, 
       null); 
     startManagingCursor(cur); 

     int size=cur.getCount(); 


     if (cur.moveToFirst()) 
     { 

      for(int i=0;i<size;i++) 
      { 
        InboxField tempInboxField = new InboxField(); 
        tempInboxField.body = cur.getString(cur.getColumnIndexOrThrow("body")); 
        tempInboxField.protocol = cur.getString(cur.getColumnIndexOrThrow("protocol")); 
        tempInboxField.type =cur.getString(cur.getColumnIndexOrThrow("type")); 
        tempInboxField.status = cur.getInt(cur.getColumnIndexOrThrow("status")); 
        tempInboxField.address =cur.getString(cur.getColumnIndexOrThrow("address")); 
        String tempdate =cur.getString(cur.getColumnIndexOrThrow("date")); 
        tempInboxField.id = cur.getInt(cur.getColumnIndexOrThrow("_id")); 
        tempInboxField.person = cur.getString(cur.getColumnIndexOrThrow("person")); 
        Long timestamp = Long.parseLong(tempdate);  
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTimeInMillis(timestamp); 
        Date finaldate = calendar.getTime(); 
        tempInboxField.date = finaldate.toString(); 
        arrayList.add(tempInboxField); 
        cur.moveToNext(); 
       } 

      } 

回答

2

可以使用刪除一條信息是:

Uri deleteUri = Uri.parse("content://sms"); 
int count = 0; 
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/" + pid; 
    count = context.getContentResolver().delete(Uri.parse(uri), 
       null, null); 
    } catch (Exception e) { 

    } 
} 

如果你想刪除一個會話線程,你可以使用這樣的:

String uri = "content://sms/conversations/" + pid; 
getContentResolver().delete(Uri.parse(uri), null, null); 

其中,pid是線程的ID。

+1

謝謝,我已經解決了我的問題,因爲它是。 –