从特定号码知道未读消息数量的方法

Way to know number of unread message from a particular number

我通过以下代码获取收件箱中未读邮件的数量。

public int getMessageCountUnread(){
        resolver = getContentResolver() ; 
        Cursor c = resolver.query(SMS_INBOX, null, "read = 0", null, null);
        int unreadMessagesCount = c.getCount();
        c.deactivate();
        return unreadMessagesCount;
    }

现在我想知道来自特定号码的未读短信的数量。我如何知道特定号码的未读短信数量?

如何设置查询?
如果我想知道没有来自特定号码的已读消息怎么办?

在您的 ContentResolver 查询中使用来自 android.provider.Telephony.TextBasedSmsColumns 的列来查找来自特定地址但未读取的 SMS 消息:

使用这些列内容:

Telephony.TextBasedSmsColumns.ADDRESS
Telephony.TextBasedSmsColumns.READ

像这样:

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

并且要知道没有来自特定号码的已读消息,只是一个小改动:

Cursor cursor = resolver.query(Telephony.Sms.CONTENT_URI,
                    null,
                    Telephony.TextBasedSmsColumns.READ + " = ? and "
                    + Telephony.TextBaseSmsColumns.ADDRESS + "= ?"   ,
                    new String[]{"true", "+1 123 123 1234"},
                    Telephony.Sms.Conversations.DEFAULT_SORT_ORDER);

更新:

如果上面的代码不起作用,使用这个:

String address = 0123456789;
Cursor unreadcountcursor = getContentResolver().query(Uri.parse("content://sms/inbox"), 
new String[]{}, "read = 0 and address='"+address+"'", null, null); 
int count = unreadcountcursor.getCount();