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();
}
}