36

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());
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Florian
  • 2,048
  • 3
  • 28
  • 36
  • possible duplicate of [How to delete an SMS from the inbox in Android programmatically?](http://stackoverflow.com/questions/419184/how-to-delete-an-sms-from-the-inbox-in-android-programmatically) – Marcin Gil Dec 23 '11 at 09:26
  • 1
    @Marclin both the questions are different. In this question SMS is geeting deleted if it is directly deployed from eclipse but not from Markrt place – Sunil Kumar Sahoo Dec 23 '11 at 09:40
  • @SunilKumarSahoo: You are right. The code works perfectly, but only if the device is connected to the PC, not if is downloaded from the market and I'm trying to understand why? – Florian Jan 04 '12 at 17:53
  • Remember to close the cursor once you are done working with it. – MikeL Mar 01 '17 at 20:16
  • @Florian - Could you please update your answer by adding a `Handler` as you mentioned. I shall be very thankful to you. – Irfan Akram Apr 24 '20 at 11:56

8 Answers8

33

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

Naan
  • 521
  • 1
  • 11
  • 28
Florian
  • 2,048
  • 3
  • 28
  • 36
16

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

Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138
10

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());
    }
  • String date = c.getString(3); 3 index is person – Deepu Apr 25 '18 at 19:10
  • Inserting, Deleting, and Updating is not allowed since KitKat 4.4 version (only allowed if the app is default SMS app or if the device is rooted). A possible workaround is found here https://stackoverflow.com/a/27709655/4445814 which supports 4.4 version only. That security hole is fixed since Lollipop 5.1. – Keshan Fernando Oct 12 '19 at 18:44
4

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.

4

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.

Cyph3rCod3r
  • 1,978
  • 23
  • 33
Kishor N R
  • 1,521
  • 1
  • 17
  • 23
  • Wouldn't simply `context.getContentResolver().delete(deleteUri, null, null);` also delete all SMSes? – Jānis Elmeris Apr 12 '17 at 05:30
  • this answer shows how to delete a single sms by _id if you know the _id then you need not to query everything, its just for reference, and yes you are right if you want to delete all sms your answer will work. – Cyph3rCod3r Sep 11 '18 at 08:57
1
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);
mehdi
  • 340
  • 4
  • 17
NikhilReddy
  • 6,904
  • 11
  • 38
  • 58
  • If you have a closer look at my code you see that this is exactly what I am doing. My code works perfectly if the device is connected to the PC with the ADB and if I deploy it directly. – Florian Jan 04 '12 at 17:51
0

I was looking for a method to delete all SMS with one click. Thanks to this post I succeeded. Here is my method if it interests someone :

    private void deleteSMS(){
    Uri myUri= Uri.parse("content://sms/");
    Cursor cursor = getContext().getContentResolver().query(myUri, null,null,null,null);
    while (cursor.moveToNext()) {
        int thread_id = cursor.getInt(1);
        getContext().getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
    }
    cursor.close();
}
Louis Chabert
  • 399
  • 2
  • 15
-1

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

Mahdi Azadbar
  • 1,330
  • 3
  • 17
  • 24