1

Hello I try to mark a SMS as read on android. I have the following code:

public void setRead(int position, String smsMessageId) {

        smsBody.get(position).status=true;
        ContentValues values = new ContentValues();
        values.put("read", true);
        context.getContentResolver().update(Uri.parse("content://sms/inbox"),
                values, "_id=" + smsMessageId, null);
    }

The execution of the code happens here:

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        try {

            SMSItem smsMessageStr = (SMSItem) arrayAdapter.getItem(pos);
            if (smsMessageStr.status == false) {

                // String smsMessageId = ((SmsArrayAdapter)
                // arrayAdapter).getId(pos);
                ((SmsArrayAdapter) arrayAdapter).setRead(pos, smsMessageStr.ID); 
                Toast.makeText(this, "ID is " + smsMessageStr.ID, Toast.LENGTH_LONG)
                        .show();
            }
            Intent intent = new Intent(SmsActivity.this,
                    ShowIndividualSMS.class);
            intent.putExtra("SMS", smsMessageStr.sms);
            startActivity(intent);

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "exception is " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e("TAG", e.getMessage());
        }
    }

I have tried all possible solutions. Why am i not able to set this sms as read? Where is the problem?

osimer pothe
  • 2,827
  • 14
  • 54
  • 92

1 Answers1

0

please find this

 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());
      }
}
Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32