Android删除发送的短信

时间:2017-08-09 09:07:37

标签: android sms smsmanager contentobserver

当我在联想A6000(5.0.2)设备中尝试以下代码时,尝试从应用删除已发送的短信

if (localStorage['FGAzXXX']) {

ConversationQueryHandler在成功删除sms on onSeletionComplete的情况下发送1,但这并不适用于所有设备。

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

我已对此进行了测试,发现无法删除以下所有设备中的短信 索尼Xperia Z1(5.1.1) 联想A200设备(5.1) 三星J210F(6.0.1)

正如我前面提到的,我能够删除具有相同代码的短信 联想A6000(5.0.2)

我有可能在这里遗漏一些东西,或者这是删除已发送的短信的正确方法。感谢您的帮助。

0 个答案:

没有答案