将 SMS 消息标记为已读/未读或删除在 KitKat 中不起作用的消息 [英] Marking SMS messages as read/unread or deleting messages not working in KitKat

查看:27
本文介绍了将 SMS 消息标记为已读/未读或删除在 KitKat 中不起作用的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 SMS 应用程序.一切都很顺利,直到昨天,当我将 Nexus 4 更新到 Android 4.4 KitKat 时.将 SMS 标记为已读/未读以及删除线程中的所有消息等功能已停止工作.为什么会这样?它适用于其他三星设备(未运行 KitKat).

这是我将消息标记为已读或未读的代码:

public static void markRead(final Context context, final Uri uri,最终读入){Log.d(TAG, "markRead(" + uri + "," + read + ")");如果(uri == null){返回;}String[] sel = Message.SELECTION_UNREAD;如果(读取 == 0){sel = Message.SELECTION_READ;}最终 ContentResolver cr = context.getContentResolver();最终的 ContentValues cv = 新的 ContentValues();cv.put(Message.PROJECTION[Message.INDEX_READ], 读);尝试 {cr.update(uri, cv, Message.SELECTION_READ_UNREAD, sel);} catch (IllegalArgumentException e) {Log.e(TAG, "更新失败", e);Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();}}

要删除线程中的所有消息,我使用:

public static void deleteMessages(final Context context, final Uri uri,最终 int 标题、最终 int 消息、最终 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,新 DialogInterface.OnClickListener() {@覆盖public void onClick(最终的DialogInterface对话框,最终 int 其中) {最终 int ret = context.getContentResolver().delete(uri, 空, 空);Log.d(TAG, "已删除:" + ret);如果(活动!= null &&!activity.isFinishing()){活动.完成();}如果 (ret > 0) {Conversation.flushCache();消息.flushCache();SmsReceiver.updateNewMessageNotification(上下文,空值);//adapter.notifyDataSetChanged();}尝试 {testFromFragment(上下文);} 捕获(异常 e){e.printStackTrace();}}});builder.show();}

解决方案

在 Android 4.4 中,SMS 方面发生了一些变化.其中一个事实是,只有注册为默认 SMS 应用程序的应用程序才具有对提供程序的写入权限.

点击此处了解有关 SMS 更改的简短介绍.>

查看此链接以获得更深入的了解.这篇文章解释了您的应用需要满足哪些标准才能成为默认消息应用.

这里是官方有趣的内容.

因此,如果您的应用不是默认消息应用,这就是指定功能停止工作的原因.

<小时>

可以在此处的答案中找到默认提供商限制的可能解决方法.

I have been working on an SMS application. Everything was smooth until yesterday, when I updated my Nexus 4 to Android 4.4, KitKat. Features such as marking an SMS as read/unread, and deleting all messages in a thread have stopped working. Why is this happening? It works on other Samsung devices (not running 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. Among them is the fact that only the app that is registered as the default SMS app has write access to the provider.

Check here for a short blurb on changes to 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.

这篇关于将 SMS 消息标记为已读/未读或删除在 KitKat 中不起作用的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆