Search code examples
androidmessageandroid-contentresolver

How to delete Multiple messages or single message from the Inbox in Android?


I access all Message from the Inbox with the help of Content Resolver But Now the problem is that I want to delete Multiple Message Or a single Message from the Inbox. I have found delete functionality for all messages not for a single message or multiple message. I store all message in a ArrayList. Any Help will be appreciated.

My code for read Message is:--

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();
                }

            }

Solution

  • You can delete a single message using this:

    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) {
    
       }
    }
    

    If you want to delete a conversation thread you can use something like this:

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

    where pid is the id of the thread.