How can I get the number of unread sms in android?
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.
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);