Marking SMS messages as read / unread or deleted messages that do not work in KitKat

I am working on an SMS application. Everything was smooth until yesterday, when I upgraded my Nexus 4 to Android 4.4, KitKat. Functions such as marking SMS as read / unread and deleting all messages in the stream stopped working. Why is this happening? It works on other Samsung devices (KitKat does not work).

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

To delete all messages in the stream, 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(); } 
+6
source share
1 answer

With Android 4.4, some things have changed regarding SMS. Among them is the fact that only an application registered as a standard SMS application has write access to the provider.

Check here for a short advertisement about changes in SMS.

Check out this link for a deeper look. This section explains what criteria your application must meet in order to be a standard messaging application.

And here is the official fun stuff.

So, if your application is not the default messaging application, this means that these functions have stopped working.


A possible workaround to restrict the default provider can be found in the answer here .

+16
source

Source: https://habr.com/ru/post/958685/


All Articles