Search code examples
javaandroidcursorsms

Query Unread Texts for Specific Contact Android


From this question I can see how to get total unread: Get number of unread sms


I have searched for this on Google but unfortunately do not understand how I may go about this.



I am wondering if it is possible to get the number of texts (unread) for a specific number, or, to do more work on my end, get the number of unread texts for each phone number in the list, and then make my own comparisons. The end result would be that I am able to, for example, take the number 111 and see how many unread tests I have from them or to simply get an ArrayList or similar construct containing numbers and unread texts for each, through which to loop and compare against the number 111.


Solution

  • Use columns from android.provider.Telephony.TextBasedSmsColumns in your ContentResolver query to find SMS messages that are from a specific address and not read:

    Use these column contants:

    Telephony.TextBasedSmsColumns.ADDRESS

    Telephony.TextBasedSmsColumns.READ

    Something like this:

    ContentResolver resolver = context.getContentResolver();
    Cursor cursor = resolver.query(Telephony.Sms.CONTENT_URI,
                    null,
                    "WHERE " + Telephony.TextBasedSmsColumns.READ + " = ? and "
                    + Telephony.TextBaseSmsColumns.ADDRESS + "= ?"   ,
                    new String[]{"false", "+1 555 555 1212"},
                    Telephony.Sms.Conversations.DEFAULT_SORT_ORDER);
    

    int unreadCount = cursor.getCount();