Home » Android » Deleting Android SMS programmatically

Deleting Android SMS programmatically

Posted by: admin April 23, 2020 Leave a comment

Questions:

I want to delete some certain SMS automatically in my Android application. Therefore I have a method which does exactly what I want it to do. However, it only works if I deploy the application directly to my phone from Eclipse. Then it deletes incoming SMS. However, it does not work if the application is downloaded from the market. But there is also no error. Does anybody know how I can solve this or does this only work on rooted devices?

public void deleteSMS(Context context, String message, String number) {
    try {
        mLogger.logInfo("Deleting SMS from inbox");
        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 address = c.getString(2);
                String body = c.getString(5);

                if (message.equals(body) && address.equals(number)) {
                    mLogger.logInfo("Deleting SMS with id: " + threadId);
                    context.getContentResolver().delete(
                        Uri.parse("content://sms/" + id), null, null);
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        mLogger.logError("Could not delete SMS from inbox: " + e.getMessage());
    }
}
How to&Answers:

Actually, the code in my post is 100% correct. The problem was that Android needs some time to store the SMS upon receiving it. So the solution is to just add a handler and delay the delete request for 1 or 2 seconds.

This actually solved the whole issue.

EDIT (thanks to Maksim Dmitriev):

Please consider that you can’t delete SMS messages on devices with Android 4.4.

Also, the system now allows only the default app to write message data to the provider, although other apps can read at any time.

http://developer.android.com/about/versions/kitkat.html

No exception will be thrown if you try; nothing will be deleted. I have just tested it on two emulators.

How to send SMS messages programmatically

Answer:

Please consider that you can’t delete SMS messages on devices with Android 4.4.

Also, the system now allows only the default app to write message data
to the provider, although other apps can read at any time.

http://developer.android.com/about/versions/kitkat.html

No exception will be thrown if you try; nothing will be deleted. I have just tested it on two emulators.

How to send SMS messages programmatically

Answer:

hey use this code to delete customize sms
1. By date
2. By number
3. By body

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);
                Log.e("log>>>",
                        "0--->" + c.getString(0) + "1---->" + c.getString(1)
                                + "2---->" + c.getString(2) + "3--->"
                                + c.getString(3) + "4----->" + c.getString(4)
                                + "5---->" + c.getString(5));
                Log.e("log>>>", "date" + c.getString(0));

                ContentValues values = new ContentValues();
                values.put("read", true);
                getContentResolver().update(Uri.parse("content://sms/"),
                        values, "_id=" + id, null);

                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[] { c.getString(4) });
                    Log.e("log>>>", "Delete success.........");
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        Log.e("log>>>", e.toString());
    }

Answer:

You can choose which app is the default SMS app in 4.4+ and if your app is set as the default it will be able to delete SMS as well.

Answer:

to make app as default app see this.
Check if your app is default sms app before deleting.
Use the URI provided by telephony class instead of hardcoding.

public void deleteSMS(Context context,int position)
{
    Uri deleteUri = Uri.parse(Telephony.Sms.CONTENT_URI);
    int count = 0;
    Cursor c = context.getContentResolver().query(deleteUri, new String[]{BaseColumns._ID}, null,
            null, null); // only query _ID and not everything
        try {
              while (c.moveToNext()) {
                // Delete the SMS
                String pid = c.getString(Telephony.Sms._ID); // Get _id;
                String uri = Telephony.Sms.CONTENT_URI.buildUpon().appendPath(pid)
                count = context.getContentResolver().delete(uri,
                    null, null);
              }
        } catch (Exception e) {
        }finally{
          if(c!=null) c.close() // don't forget to close the cursor
        }

   }

it delete all(inbox,outbox,draft) the SMS.

Answer:

private int deleteMessage(Context context, SmsMessage msg) {
    Uri deleteUri = Uri.parse("content://sms");
    int count = 0;
            @SuppressLint("Recycle") Cursor c = context.getContentResolver().query(deleteUri, null, null, null, null);

    while (c.moveToNext()) {
        try {
            // Delete the SMS
            String pid = c.getString(0); // Get id;
            String uri = "content://sms/" + pid;
            count = context.getContentResolver().delete(Uri.parse(uri),
                    null, null);
        } catch (Exception e) {
        }
    }
    return count;
}

use this code………….

or

getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);

Answer:

if you want get a message and your sms app your android device phone didn’t send any notification use Binary (Data) SMS.