Andrey Koltsov
Andrey Koltsov

Reputation: 1974

Android: how to mark sms as read in onReceive

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?

Upvotes: 12

Views: 14316

Answers (5)

user4215456
user4215456

Reputation: 79

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

Upvotes: 0

Name is Nilay
Name is Nilay

Reputation: 2783

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

Upvotes: 28

xCh3Dx
xCh3Dx

Reputation: 128

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

Upvotes: 9

Anton Kaiser
Anton Kaiser

Reputation: 723

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!

Upvotes: 6

jmif
jmif

Reputation: 1212

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

Upvotes: 0

Related Questions