12

I can catch sms, can see sender phone, body, I can abortBroadcast if I don't like this sms, but I don't know how to just mark this sms as read, that user can readit in box later. Any ideas how I can do this?

AsTheWormTurns
  • 1,318
  • 3
  • 13
  • 26
Andrey Koltsov
  • 1,956
  • 6
  • 24
  • 43

5 Answers5

28

This might help you :

private void markMessageRead(Context context, String number, String body) {

            Uri uri = Uri.parse("content://sms/inbox");
            Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
            try{

            while (cursor.moveToNext()) {
                    if ((cursor.getString(cursor.getColumnIndex("address")).equals(number)) && (cursor.getInt(cursor.getColumnIndex("read")) == 0)) {
                        if (cursor.getString(cursor.getColumnIndex("body")).startsWith(body)) {
                            String SmsMessageId = cursor.getString(cursor.getColumnIndex("_id"));
                            ContentValues values = new ContentValues();
                            values.put("read", true);
                            context.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=" + SmsMessageId, null);
                            return;
                        }
                    }
                }
      }catch(Exception e)
      {
          Log.e("Mark Read", "Error in Read: "+e.toString());
      }
}
Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
9

Since Android 4.4 KitKat the only app can modify sms data - SMS-app that was set as default

only the app that receives the SMS_DELIVER_ACTION broadcast (the user-specified default SMS app) is able to write to the SMS Provider defined by the android.provider.Telephony class and subclasses

More info can be found here: http://android-developers.blogspot.ru/2013/10/getting-your-sms-apps-ready-for-kitkat.html

tchelidze
  • 8,050
  • 1
  • 29
  • 49
xCh3Dx
  • 128
  • 1
  • 6
6

An answer has been given here: Set sms as read in Android

ContentValues values = new ContentValues();
values.put("read",true);
getContentResolver().update(Uri.parse("content://sms/inbox"),values,
    "_id="+SmsMessageId, null);

where "_id" is the message's ID

Edited, thanks NilayOnAndroid!

Community
  • 1
  • 1
Anton Kaiser
  • 713
  • 6
  • 11
0

I did a workarround for versions newer that KitKat, from answer here: if there are messages to be read, when user leaves the app, start SMS app with the number for which I want messages to mark read. This will automatically mark all messages as read.

@Override
public void onBackPressed() {
  if (toBeRead) {
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    smsIntent.putExtra("address", smsNumber);
    try {startActivity(smsIntent);}
    catch (Exception e) {
      try {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("smsto:" + Uri.encode(smsNumber)));
        startActivity(intent);
      }
      catch (Exception e1) {}
    }
    toBeRead = false;
  }
}
  • what is "smsNumber" and where does it get its value from? should it be equal the _id column's value? or something else? – Yoav Feuerstein Jul 03 '18 at 11:46
  • Also, are you sure this works on current-day Android versions? see here: https://stackoverflow.com/q/19853220/997940 – Yoav Feuerstein Jul 03 '18 at 13:54
  • @Yoav Feuerstein well "smsNumber" is pretty clear what it is ("the number for which you want messages to mark read"). As for the other comment, you are right about that, see my updated answer – user4215456 Jul 04 '18 at 20:18
0

I don't think that there is any official support for this, but this question provides a method of doing it (have not tried it though): Mark MMS as read programmatically

Community
  • 1
  • 1
jmif
  • 1,192
  • 2
  • 14
  • 22