Search code examples
androidsmsnotificationsmessageled

Checking if a new SMS has been read


I am working on a simple app for the HTC EVO that blinks the alternate notification LED when a new text message is received. I have this part working great via a Broadcast Receiver but I need some way to turn the LED off when the user has read the message(s) using their default SMS app. I'm not sure if it is best to do this in the receiver or in a background service. I found this, which might be what I am looking for, but I have no idea on how to use it as I could not find any instructions or tutorials.


Solution

  • Alright, I have worked out the following code which I think will meet my needs.

    private int getUnreadSMSCount()
    {
        int count = 0;
        Uri smsURI = Uri.parse("content://sms");
        ContentResolver contentResolver = this.getContentResolver();
        Cursor cursor = contentResolver.query(smsURI, null, "read=0", null, null);
        if (cursor != null)
        {
            try
            {
                count = cursor.getCount();
            }
            finally
            {
                cursor.close();
            }
        }
    
        return count;
    

    }