Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to mark sms as read in onReceive

Tags:

android

sms

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?

like image 340
Andrey Koltsov Avatar asked Dec 26 '11 16:12

Andrey Koltsov


People also ask

Which method is fired when incoming SMS message is received?

Receive SMS messages with a broadcast receiver. To receive SMS messages, use the onReceive() method of the BroadcastReceiver class. The Android framework sends out system broadcasts of events such as receiving an SMS message, containing intents that are meant to be received using a BroadcastReceiver.


2 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());
      }
}
like image 69
Name is Nilay Avatar answered Oct 03 '22 06:10

Name is Nilay


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

like image 20
xCh3Dx Avatar answered Oct 03 '22 06:10

xCh3Dx