Delete an sms from inbox
Asked Answered
C

2

7

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

Companionway answered 22/2, 2012 at 5:51 Comment(0)
T
12

You can use following method for deleting SMS from Inbox,

private void deleteMessage()
{
    Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null); 
    //c.moveToFirst(); 

    while (c.moveToNext())
    {
        System.out.println("Inside if loop");

        try
        {
            String address = c.getString(2);
            String MobileNumber = mainmenu.getParameterData().getMobileNumber().trim();

            //Log.i( LOGTAG, MobileNumber + "," + address );

            Log.i( LOGTAG, c.getString(2) );


            if ( address.trim().equals( MobileNumber ) )
            {
                String pid = c.getString(1);
                String uri = "content://sms/conversations/" + pid;
                getContentResolver().delete(Uri.parse(uri), null, null);
                stopSelf();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    } 
}
Tetrapod answered 22/2, 2012 at 5:55 Comment(1)
Will this work in 5.0.? Using the same code is returning my cursor empty.Elena
U
11

try this for complete solution for deleting...

    public void deleteSMS(Context context, String message, String number) {
    try {
        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(
                uriSms,
                new String[] { "_id", "thread_id", "address", "person",
                        "date", "body" }, "read=0", null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String address = c.getString(2);
                String body = c.getString(5);
                String date = c.getString(3);
                Log.e("log>>>",
                        "0>" + c.getString(0) + "1>" + c.getString(1)
                                + "2>" + c.getString(2) + "<-1>"
                                + c.getString(3) + "4>" + c.getString(4)
                                + "5>" + c.getString(5));
                Log.e("log>>>", "date" + c.getString(0));

                if (message.equals(body) && address.equals(number)) {
                    // mLogger.logInfo("Deleting SMS with id: " + threadId);
                    context.getContentResolver().delete(
                            Uri.parse("content://sms/" + id), "date=?",
                            new String[] { c.getString(4) });
                    Log.e("log>>>", "Delete success.........");
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        Log.e("log>>>", e.toString());
    }
}
Unwept answered 20/3, 2013 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.