0

In our application there is a feature delete SMS programmatically. The application Target SDK version is 26 . And i have changed default SMS application as default SMS app which is available on device. I have tried the code below.

Requirement: Need code for delete all sms and MMS

First one:

public boolean deleteSms() {
        Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), new String[]{"_id", "thread_id", "address", "person", "date", "body"}, null, null, null);
        try {
            while (c.moveToNext()) {
                int id = c.getInt(0);
                String address = c.getString(2);
                this.getContentResolver().delete(
                        Uri.parse("content://sms/" + id), null, null);
                isSmsDeleted = true;
            }

        } catch (Exception ex) {
            isSmsDeleted = false;
        }
        return isSmsDeleted;
    }

and I have tried another one function that is

private int deleteSms() {
        try {
            ContentResolver localContentResolver = context.getContentResolver();
            Uri localUri = Uri.parse("content://sms");
            return localContentResolver.delete(localUri, null, null);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return 0;
    }

My Manifest Permission is

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

When i debug the above code the return localContentResolver.delete(localUri, null, null); it returns 0. And both functionality is not working

Tested Device : Google Pixel 2Xl version 8.1 and MIUI Redmi 5 version 7.1.2

karthik
  • 321
  • 1
  • 8
  • 21
  • "And i have changed default SMS application as default SMS app which is available on device." – I'm not sure what you're saying there. Do you mean that your app is set as the default, or no? – Mike M. Aug 03 '18 at 09:42
  • I have set the device message application as default sms., Because i want to delete message from default sms application which is available on device. @MikeM. – karthik Aug 03 '18 at 09:44
  • You can't delete messages then. Since KitKat (4.4), only the default app has write access to the Provider. – Mike M. Aug 03 '18 at 09:46

0 Answers0