Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 890
playmaker420 Avatar asked Nov 23 '13 05:11

playmaker420


1 Answers

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.

like image 68
Mike M. Avatar answered Jan 03 '23 19:01

Mike M.