0

Trying to delete the sent sms from app, when I have tried below code in Lenovo A6000(5.0.2) device

public static void deleteMessage(Context context, String phoneNo, String message) {
        try {
            Log.d(TAG, "deleteMessage: Deleting SMS from inbox");
            Uri uriSms = Uri.parse("content://sms/");
            Cursor c = context.getContentResolver().query(uriSms,
                    new String[]{"_id", "thread_id", "address",
                            "person", "date", "body"}, null, null, null);
            Uri uri = 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);
                    int rowsDeleted = 0;
                    Log.d(TAG, "Deleting threads: " + threadId);
                    Log.d(TAG, "deleteMessage: id- "+ id + "" +
                            " threadId- " + threadId + "" +
                            " body- " + body + "" +
                            " rowsDeleted- " + rowsDeleted + "" +
                            " address- " + address);
                    if (address.equalsIgnoreCase(phoneNo)
                            && body.equalsIgnoreCase(message)) {
                        ConversationQueryHandler handler = new ConversationQueryHandler(context.getContentResolver(), context);
                        synchronized (sDeletingThreadsLock) {
                            Log.v(TAG, "Conversation startDelete sDeletingThreads: " + sDeletingThreads);
                            if (sDeletingThreads) {
                                Log.e(TAG, "startDeleteAll already in the middle of a delete", new Exception());
                            }
                            sDeletingThreads = true;
                            uri = ContentUris.withAppendedId(Telephony.Threads.CONTENT_URI, threadId);
                            String selection = true ? null : "locked=0";

                            handler.setDeleteToken(0);
                            handler.startDelete(0, new Long(threadId), uri, selection, null);
                        }
                    }
                } while (c.moveToNext());
            }
        } catch (Exception e) {
            Log.d(TAG, "deleteMessage: Could not delete SMS from inbox: " + e.getMessage());
        }
    }

The ConversationQueryHandler sends 1 as a result in case of successful deletion of sms on to onDeletionComplete but this doesn't work in all the devices.

    private static Object sDeletingThreadsLock = new Object();
private static boolean sDeletingThreads;

public static class ConversationQueryHandler extends AsyncQueryHandler {
    private int mDeleteToken;
    private Context mContext;

    public ConversationQueryHandler(ContentResolver cr, Context context) {
        super(cr);
        mContext = context;
    }

    public void setDeleteToken(int token) {
        mDeleteToken = token;
    }

    /**
     * Always call this super method from your overridden onDeleteComplete function.
     */
    @Override
    protected void onDeleteComplete(int token, Object cookie, int result) {
    Log.v(TAG, "Conversation onDeleteComplete token: " + token + " cookie- " + cookie + " result- " + result);
        if (token == mDeleteToken) {
            // release lock
            synchronized (sDeletingThreadsLock) {
                sDeletingThreads = false;

                sDeletingThreadsLock.notifyAll();
            }
        }
    }
}

I have tested this and found it is failed to delete the sms in all the below devices Sony Xperia Z1(5.1.1) Lenovo A200 device (5.1) Samsung J210F (6.0.1)

As I mentioned earlier I am able to delete sms with the same code in Lenovo A6000(5.0.2)

Is there a chance I am missing something here, or is this a right way of deleting the sent sms. Thank you for the help in advance.

srikanth
  • 308
  • 3
  • 13
  • Since KitKat (4.4), only the app selected as the default messaging app should have write access to the SMS Provider. Other apps should not be able to delete messages. I'm not sure why you're able to on that particular Lenovo. – Mike M. Aug 09 '17 at 09:12
  • Yes Mike M, as per official documentation we can not delete, is there any way to prevent the sent sms being shown in inbox or sms messages. Since my sent SMS contains a sensitive information, i have to prevent it from user visibility. – srikanth Aug 09 '17 at 09:16
  • On a standard device, no. The system handles writing sent messages to the Provider for all non-default apps, and once you call the `SmsManager` method, it's out of your hands. – Mike M. Aug 09 '17 at 09:20
  • I would mention, though, that data SMS are not saved to the Provider, so if you can use that instead, those messages won't show in any other app. – Mike M. Aug 09 '17 at 09:22
  • Sorry Mike, I couldn't able to reach you on above comment, you mean i can prevent the SMS being shown to user by preventing writing SMS to Provider. – srikanth Aug 09 '17 at 09:35
  • For text SMS, you cannot prevent the system from saving the messages. Data SMS, however, are not saved to the Provider, so those won't show in any other app. – Mike M. Aug 09 '17 at 09:38
  • Can you please suggest me how can i send the data SMS. – srikanth Aug 09 '17 at 09:44
  • Yep, I was just looking for [this post](https://stackoverflow.com/questions/3757229), which has examples of both sending and receiving in the answer. – Mike M. Aug 09 '17 at 09:46
  • Thank you Mike, I have looked into the post, and tried sendDataMessage() only sends the text message as bytes which is not compatible with our reciever program, can we able to send this as a string in any other way. – srikanth Aug 09 '17 at 10:45
  • 1
    Nope, that's just how the data SMS API works. That's kinda why I said "_if_ you can use that", 'cause I didn't know the details of what you're sending these messages to. – Mike M. Aug 09 '17 at 10:50
  • 1
    Thank you for the help Mike. – srikanth Aug 09 '17 at 12:29

0 Answers0