Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

android - Marking SMS messages as read/unread or deleting messages not working in KitKat

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();
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...