Android: how to mark sms as read in onReceive

2019-01-14 03:46发布

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?

标签: android sms
5条回答
Evening l夕情丶
2楼-- · 2019-01-14 04:14

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());
      }
}
查看更多
smile是对你的礼貌
3楼-- · 2019-01-14 04:20

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!

查看更多
家丑人穷心不美
4楼-- · 2019-01-14 04:20

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;
  }
}
查看更多
The star\"
5楼-- · 2019-01-14 04:27

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

查看更多
爷的心禁止访问
6楼-- · 2019-01-14 04:29

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

查看更多
登录 后发表回答