Unable to delete SMS programmatically

2019-07-04 16:09发布

问题:

I have tried variations on deleting all or some messages from the SMS, including but not limited to thread id, id, where clause, etc. Android manifest reflects read and write permissions. I tried with variations on the SMS content, from inbox etc. Nothing seems to delete the record.

Here is the last iteration:

    Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), null, null, null,null);
    try {
          while (c.moveToNext()) {
             int Id = c.getInt(0);
             String pid = c.getString(0);
            // String uri = "content://sms/conversations/" + threadId;
             String strUriAll = "content://sms/" + pid;//SMS_ALL
             Log.d("URI is ", strUriAll);
             getApplicationContext().getContentResolver().delete(Uri.parse(strUriAll), null, null);
          //   getApplicationContext().getContentResolver().delete(Uri.parse(strUriAll), null, null);
          }

        }catch(Exception e){
             Log.e(this.toString(),"Error deleting sms",e);
        }finally {
          c.close();
        }

回答1:

Android 4.4 Kitkat introduced changes in SMS handling where now only the default SMS app is allowed write access to the SMS database - all other apps silently fail when attempting to write.



标签: android sms