Get number of unread sms

后端 未结 2 1992
礼貌的吻别
礼貌的吻别 2020-12-03 07:40

How can I get the number of unread sms in android?

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 08:10

    Need to execute a simple query to SMS ContentProvider. Here is a working example:

    final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
    
    Cursor c = getContentResolver().query(SMS_INBOX, null, "read = 0", null, null);
    int unreadMessagesCount = c.getCount();
    c.deactivate();
    

    You will also need the READ_SMS permission:

    
    

    Keep in mind that the SMS content provider isn't actually part of the SDK, and this code is not guaranteed to work on all past, present and future devices.

提交回复
热议问题