Search code examples
androidsmsandroid-contentresolver

How to delete SMS programatically in android


I want to delete 1 SMS from my android device programatically. Here is the code I am currently using.

private void DeleteSMS(int SmsId){
         Cursor c = getContentResolver().query(Uri.parse("content://sms/"),new String[] {"_id", "thread_id", "address", "body" }, null, null, null);
         while (c.moveToNext()) {
            try {
                    int pid = Integer.valueOf(c.getString(0));; // Get id;
                    String smsMessage = c.getString(3);
                    if (pid == SmsId)
                    {
                        String uri = "content://sms/"+pid;
                        int rows = getContentResolver().delete(Uri.parse(uri), null, null);
                        Toast.makeText(context, rows+" Message Deleted", Toast.LENGTH_LONG).show();
                        break;
                    }
            } catch (Exception e) {
                    Log.v("exception","occurred");
            }
         }
    }

After executing the delete statement with getContentResolver().delete, it will return the rows affected as 0.


Solution

  • Pls check this answer https://stackoverflow.com/a/8851644/3020568

    Your app should be configured as default SMS app for dealing with sms - above 4.4

    Check this

    http://android-developers.blogspot.in/2013/10/getting-your-sms-apps-ready-for-kitkat.html