Mark SMS as read

brelto85

Active Member
Licensed User
Longtime User
I have an app that reads sms messages, but how do i mark it as read, so the system wont think it is new?
 

eleandrot

Member
Licensed User
Longtime User
Tenho o mesmo problema
consigo ler etc
mas nao consigo marcar como lida
preciso de ajuda

i have this problem
i read but dont mark as read

help
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
is it possible to mark the sms as read (or remove the icon of new sms)
after the interception? (I don't want to return true)
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Hi Shay, brelto85,

I believe it can be done, have a search and look for ContentResolver library (Erel) in order to implement the following codes in B4A.

From stackoverflow:

B4X:
public static boolean setMessageRead(Context context, long messageID, boolean isViewed){
    try{
        if(messageID == 0){
            return false;
        }
        ContentValues contentValues = new ContentValues();
        if(isViewed){
            contentValues.put("READ", 1);
        }else{
            contentValues.put("READ", 0);
        }
        String selection = null;
        String[] selectionArgs = null;   
        _context.getContentResolver().update(
                Uri.parse("content://sms/" + messageID),
                contentValues,
                selection,
                selectionArgs);
        return true;
    }catch(Exception ex){
        return false;
    }
}

Or from another post:

B4X:
ContentValues values = new ContentValues();
values.put("read",true);
getContentResolver().update(Uri.parse("content://sms/inbox"),values,
    "_id="+SmsMessageId, null);

Though I did not try it yet. Somebody say uri must be content://sms/inbox, but others say content://sms. I think you can try yourself. You may also need to add permissions in the Manifest to read and write SMS.
 
Last edited:
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Hi Shay, brelto85,

I am trying to help you so I tried to implement the codes said above in B4A.
The following codes successfully update a message from "Unread" to "Read".
I know my codes are not yet clean, but I don't have much time...

To find a message Id, you need to use Phone library:
(Note. Adapt it as per your need, or you may want to use the SmsInterceptor. In my case, I use an Unread message that is EXISTING in the Inbox to test)
This sub is finding out the message Id among the ones in the last 30 days.
B4X:
Sub FindSmsId(MessageAddress as String) As Long
    Dim messageId As Long
    Dim SmsMessages1 As SmsMessages
    Dim smsList As List
    smsList.Initialize
    Dim mySms As Sms
    smsList = SmsMessages1.GetAllSince(DateTime.add(DateTime.Now, 0, 0, -30))
    If smsList.Size > 0 Then
        For i = 0 To smsList.Size - 1
            mySms = smsList.Get(i)
            If mySms.Address = MessageAddress Then
                messageId = mySms.Id
                Return messageId
            End If
        Next
    End If
End Sub

With the message Id then you can change it to "READ" (or vice versa) with the following function - do not forget to add permission in your Manifest editor - using ContentResolver library:
B4X:
Sub MarkSmsAsRead(messageId As Long)

      Dim u As Uri
      u.Parse("content://sms/")
      Dim crsr As Cursor = cr.Query(u, Array As String("_id"), "_id = ?", Array As String(messageId), "")
      If crsr.RowCount = 0 Then
          Log("No match found: " & messageId)
      Else
        crsr.Position = 0
        Dim cv As ContentValues
        cv.Initialize
        cv.PutBoolean("read", True)
        cr.Update(u, cv, "_id = " & messageId,Null)
      End If
      crsr.Close

End Sub

It works 100% in my case. Test it and update us.

Edit:
Note. The codes above cannot help you to clear the Unread message notification in the Notification bar. It will still stay there.
 
Last edited:
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Hi Shay: Do you mean when you change SMS from Unread (new) to Read with the code above, it does clear the new message's notification?
What is your test device? What version of Android it is running? I ask so as in my case, the notification is still staying there (of course, unless you turn off the notification in sms settings before receiving).
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
yes
Galaxy S2
4.04

The code is bit different than you wrote ( added dim cr)

B4X:
Sub MarkSmsAsRead(messageId As Long)
Dim u As Uri
Dim cr As ContentResolver
cr.Initialize("cr")
u.Parse("content://sms/")
Dim crsr As Cursor = cr.Query(u, Array As String("_id"), "_id = ?", Array As String(messageId), "")
If crsr.RowCount = 0 Then
  Log("No match found: " & messageId)
Else       
  crsr.Position = 0
  Dim cv As ContentValues
  cv.Initialize
  cv.PutBoolean("read", True)
  cr.Update(u, cv, "_id = " & messageId,Null)
End If
crsr.Close
End Sub
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Yes, I made "Dim cr As ContentResolver" in Sub Globals in my demo program.
My version is 4.1, so I think from 4.1 we cannot clear the Notification programmatically anymore.
There is another thread about this in B4A forum.
 
Upvote 0

KY Leng

Member
Licensed User
Longtime User
yes
Galaxy S2
4.04

The code is bit different than you wrote ( added dim cr)

B4X:
Sub MarkSmsAsRead(messageId As Long)
Dim u As Uri
Dim cr As ContentResolver
cr.Initialize("cr")
u.Parse("content://sms/")
Dim crsr As Cursor = cr.Query(u, Array As String("_id"), "_id = ?", Array As String(messageId), "")
If crsr.RowCount = 0 Then
  Log("No match found: " & messageId)
Else      
  crsr.Position = 0
  Dim cv As ContentValues
  cv.Initialize
  cv.PutBoolean("read", True)
  cr.Update(u, cv, "_id = " & messageId,Null)
End If
crsr.Close
End Sub

Hi Shay, you code work for my android 4.1.2 and cannot clear the Notification message.
However it still good. But when I upgrade to 4.4.2, I cannot mark the message as read, neither the notification message.

Do you have any workable code?

Best regards,
 
Upvote 0
Top