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

- 2,743
- 4
- 35
- 77
-
1No, just a particular message with _id = SmsMessageId – Name is Nilay Jul 16 '15 at 11:46
-
The return result of update query is 0 . What is the reason behind this ? – osimer pothe Sep 30 '15 at 05:22
-
1http://stackoverflow.com/questions/32858714/the-return-value-of-update-query-is-0 – osimer pothe Sep 30 '15 at 05:55
-
@NameisNilay why using `startsWith` instead of `equals` here :`cursor.getString(cursor.getColumnIndex("body")).startsWith(body)` – tchelidze Feb 13 '17 at 09:03
-
1Note that it is safer to check the result (int) of the call to update(), should be 1. Also, perhaps it won't let you do that unless you're the default messaging app on the phone? – Yoav Feuerstein Jul 03 '18 at 11:54
-
which permission should add in manifest to put a value in a sms – mehmet Aug 18 '18 at 10:33
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
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!

- 1
- 1

- 713
- 6
- 11
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;
}
}

- 79
- 5
-
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
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
-
wrong idea, sms that I catch in onReceive isn't in "content://sms" – Andrey Koltsov Dec 27 '11 at 03:14