0

While upgrading my app into higher version of Android, I'm confused why I cannot delete SMS message on Android version 7.0 to 9.0. It works fine in Android version 6.0.

Why can't I delete all messages in Android version 9.0?

public void deleteSMS(Context context) {
    String message ="Notifier,";
    try {
        Toast.makeText(context, "Deleting SMS from inbox", Toast.LENGTH_SHORT).show();
        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(uriSms,
                new String[] { "_id", "thread_id", "address",
                        "person", "date", "body" }, null, null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String body = c.getString(5);

                if (message.equals(body)) {
                    Toast.makeText(context, "Deleting SMS with id: " + threadId, Toast.LENGTH_SHORT).show();
                    System.out.println ( threadId );

                    context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        Toast.makeText(context, "Could not delete SMS from inbox: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
McLarvel
  • 31
  • 6
  • This might help you https://stackoverflow.com/questions/8614211/deleting-android-sms-programmatically – Cyrille Con Morales Aug 13 '20 at 02:58
  • @CyrilleConMorales Can you explain please the idea because I already used the code from that link, but I don't get the point why I cant delete messages. Thank you so much – McLarvel Aug 14 '20 at 00:50
  • Since KitKat (Android 4.4), only the default SMS app has write access to the Provider, so only it can delete messages. I'm guessing that you've not configured and set your app as the default SMS app, which would be why it doesn't work on "Android version 7.0 to 9.0", but that doesn't jibe with your statement that it's working on "Android version 6.0", so I'm not sure if those versions are accurate. – Mike M. Sep 06 '20 at 17:29
  • @MikeM. I've been confused about default SMS app, Do you mean ? I need to change the default app to my settings? and set my app to default message? – McLarvel Sep 06 '20 at 17:38
  • @MikeM. It is necessary to write a program? in order to change the default. In my phone settings my application didnot show in the list of default messaging app. thanks for your response – McLarvel Sep 06 '20 at 17:44
  • The user would need to select your app as the default SMS app. To be eligible to be selected as the default, your app must be configured a certain way, like is described in [this answer](https://stackoverflow.com/a/30133663). I would also mention that the default app is responsible for many, many things – e.g., handling incoming and outgoing SMS and MMS messages, dealing with relevant external `Intent`s, displaying notifications, managing storage of all messages, etc. – so a user is unlikely to select an app that is not a full-blown messaging client as their default. – Mike M. Sep 06 '20 at 17:48
  • Thanks sir @MikeM. I'm gonna try this, but sir, can I ask you questions about biometrics android phone? just want some ideas, please where can we communicate? – McLarvel Sep 06 '20 at 18:01
  • Sorry, I haven't done anything with biometrics yet. – Mike M. Sep 06 '20 at 18:14
  • @MikeM. Just one question. Can android support external fingerprint scanners? Is there any documentation command/syntax in it? Im talk about hardware biometrics fingerprint attach to android phone . Im gonna delete my comment later since this is not the main topic . thanks for your response. – McLarvel Sep 06 '20 at 18:20
  • Sorry, I really don't know. I don't even use biometrics on my own device. – Mike M. Sep 06 '20 at 18:21
  • @MikeM. Thank you! – McLarvel Sep 07 '20 at 02:20
  • @MikeM. Hi Mr. mike do you know what's the problem if I run my app in android version 9.0 it's populate like this? [link](https://i.stack.imgur.com/uoYcL.png) it restrict the sms on phone , thanks in advance! – McLarvel Oct 16 '20 at 08:19
  • Can anyone explain to me why it restrict the permission in android version 9? – McLarvel Oct 17 '20 at 09:35
  • @MikeM. Please notify , even if you don't know thanks :/ – McLarvel Oct 21 '20 at 01:25

1 Answers1

-2

From the documentation, the delete() methods requires three parameters. The first parameter you supplied is wrong. Try this:

contentResolver.delete(Telephony.Sms.CONTENT_URI, "_id=" + Long.toString(i), null);

Raymond Salim
  • 57
  • 1
  • 6
  • 1
    Still doesn't work :( . Can anyone tell me or explain me why does not delete message in the higher version? please – McLarvel Sep 06 '20 at 17:18