How to mark all text messages as read on Android?

时光怂恿深爱的人放手 提交于 2019-12-12 03:22:58

问题


I'm trying to mark all text messages as read when user opens my inbox. I've pieced together code from a few tutorials online and ended up with this:

 Uri uri = Uri.parse("content://sms/inbox");
     Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    while (cursor.moveToNext()) {
        if ((cursor.getInt(cursor.getColumnIndex("read")) == 0)) {
                String SmsMessageId = cursor.getString(cursor.getColumnIndex("_id"));
                ContentValues values = new ContentValues();
                values.put("read", true);
                getContentResolver().update(Uri.parse("content://sms/inbox"), values, "read=0", null);
            }

I just want to mark all text messages as read in the onResume() function on this activity. My code may be a pile of crap, like i said it's mashed together from a few places. Corrections to, or alternatives to, my code would be very appreciated. Compiling the code with the sdk for 5.1, testing on 4.4, my app is the default SMS app.


回答1:


If you want to mark all messages as read, you can do that in one go.

ContentValues values = new ContentValues();
values.put(Telephony.Sms.READ, 1);
getContentResolver().update(Telephony.Sms.Inbox.CONTENT_URI, 
    values, Telephony.Sms.READ + "=0", null);


来源:https://stackoverflow.com/questions/33678063/how-to-mark-all-text-messages-as-read-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!