Get number of unread sms

后端 未结 2 1981
礼貌的吻别
礼貌的吻别 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:

    <uses-permission android:name="android.permission.READ_SMS" />
    

    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.

    0 讨论(0)
  • 2020-12-03 08:29

    The simplest way I found out:

    Cursor c = getContentResolver().query(
        Uri.parse("content://sms/inbox"),
        new String[] {
            "count(_id)",
        },
        "read = 0",
        null,
        null
    );
    c.moveToFirst();
    int unreadMessagesCount = c.getInt(0);
    
    0 讨论(0)
提交回复
热议问题