Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect SMS deletion in android?

Is it possible in Android to detect that a message has been deleted from the message store in Android programatically. If yes, do we have access to information that which SMS was deleted.

like image 800
Robin Avatar asked Jan 30 '26 20:01

Robin


2 Answers

The best I can think of is that you can register a ContentObserver on the SMS/MMS content provider (I think it is content://mms-sms) and whenever a change occurs you'll get a callback. Note that you will need to scan the ContentProvider and save its current state and then every time there is a change you will need to search the ContentProvider to figure out WHAT changed: there is no pre-packaged way to be informed that the user deleted a specific message.

like image 162
Femi Avatar answered Feb 02 '26 08:02

Femi


Simply use this code

try {
        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(
                uriSms,
                new String[] { "_id", "thread_id", "address", "person",
                        "date", "body" }, "read=0", null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String address = c.getString(2);
                String body = c.getString(5);
                String date = c.getString(3);
                if (message.equals(body) && address.equals(number)) {
                    // mLogger.logInfo("Deleting SMS with id: " + threadId);
                    context.getContentResolver().delete(
                            Uri.parse("content://sms/" + id), "date=?",
                            new String[] { <your date>});
                    Log.e("log>>>", "Delete success.........");
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        Log.e("log>>>", e.toString());
    }
like image 35
Ashekur Rahman Molla Asik Avatar answered Feb 02 '26 08:02

Ashekur Rahman Molla Asik