Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get number of unread sms

Tags:

android

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

like image 690
Tiago Costa Avatar asked Sep 15 '10 14:09

Tiago Costa


People also ask

How do I see unread messages on Iphone?

All replies. Open Settings ➡️ Notifications ➡️ Messages ➡️ Badges: Ensure that this is set to ON.

How do you check how many messages you have on Android?

1 Tap Notification Settings on the notification panel or tap the Settings app. 2 Tap Notifications. 3 Tap App icon badges. 4 Select Show with number.

Why is the message counter showing one unread message when there are no unread messages in an Android mobile?

If your Android is constantly notifying you of new or unread text messages that don't exist, it's usually due to your messaging app's cached or saved data. Sometimes these issues will clear up automatically upon receipt of a new message, so try asking someone to send you a message first.

Is there a way to delete all unread texts?

To delete bulk messages, open the Google Messages app on your Android device and make sure the app is set as default on your device. Inside the app, select the message type from the tabs at the top (All, Personal, Transactions, OTPs, and Offers). This will let you find the messages you want to delete much faster.


2 Answers

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.

like image 159
Sergey Glotov Avatar answered Nov 02 '22 21:11

Sergey Glotov


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);
like image 41
alopatindev Avatar answered Nov 02 '22 21:11

alopatindev