简体   繁体   English

将 SMS 消息标记为已读/未读或删除在 KitKat 中不起作用的消息

[英]Marking SMS messages as read/unread or deleting messages not working in KitKat

I have been working on an SMS application.我一直在研究 SMS 应用程序。 Everything was smooth until yesterday, when I updated my Nexus 4 to Android 4.4, KitKat.一切都很顺利,直到昨天,当我将 Nexus 4 更新到 Android 4.4 KitKat 时。 Features such as marking an SMS as read/unread, and deleting all messages in a thread have stopped working.将 SMS 标记为已读/未读以及删除线程中的所有消息等功能已停止工作。 Why is this happening?为什么会这样? It works on other Samsung devices (not running KitKat).它适用于其他三星设备(不运行 KitKat)。

This is my code to mark a message as read or unread:这是我将消息标记为已读或未读的代码:

public static void markRead(final Context context, final Uri uri,
            final int read) {
        Log.d(TAG, "markRead(" + uri + "," + read + ")");
        if (uri == null) {
            return;
        }
        String[] sel = Message.SELECTION_UNREAD;
        if (read == 0) {
            sel = Message.SELECTION_READ;
        }
        final ContentResolver cr = context.getContentResolver();
        final ContentValues cv = new ContentValues();
        cv.put(Message.PROJECTION[Message.INDEX_READ], read);
        try {
            cr.update(uri, cv, Message.SELECTION_READ_UNREAD, sel);
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "failed update", e);
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
        }
}

For deleting all messages in a thread, I use:为了删除线程中的所有消息,我使用:

public static void deleteMessages(final Context context, final Uri uri,
            final int title, final int message, final Activity activity) {

        Log.i(TAG, "deleteMessages(..," + uri + " ,..)");
        final Builder builder = new Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setNegativeButton(android.R.string.no, null);
        builder.setPositiveButton(android.R.string.yes,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog,
                            final int which) {
                        final int ret = context.getContentResolver().delete(
                                uri, null, null);
                        Log.d(TAG, "deleted: " + ret);
                        if (activity != null && !activity.isFinishing()) {
                            activity.finish();
                        }
                        if (ret > 0) {
                            Conversation.flushCache();
                            Message.flushCache();
                            SmsReceiver.updateNewMessageNotification(context,
                                    null);
                            // adapter.notifyDataSetChanged();
                        }
                        try {
                            testFromFragment(context);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
        builder.show();
}

With Android 4.4, several things have changed with regard to SMS.在 Android 4.4 中,SMS 方面发生了一些变化。 Among them is the fact that only the app that is registered as the default SMS app has write access to the provider.其中一个事实是,只有注册为默认 SMS 应用程序的应用程序才具有对提供程序的写入权限。

Check here for a short blurb on changes to SMS.在此处查看有关 SMS 更改的简短介绍。

Check this link for a more in depth look. 检查此链接以获得更深入的了解。 This one explains what criteria your app needs to meet to be the default messaging app.这个解释了您的应用程序需要满足哪些标准才能成为默认消息应用程序。

And here's the official fun stuff. 是官方有趣的东西。

So, if your app is not the default messaging app, that would be why the specified functionalities have stopped working.因此,如果您的应用程序不是默认的消息传递应用程序,这就是指定功能停止工作的原因。


A possible workaround for the default Provider restriction can be found in the answer here .可以在此处答案中找到默认提供程序限制的可能解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM