Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

count - How to get the number of unread gmail mails (on android)

Please note there is a new way of doing this

I've been trying to get the number of unread gmail mails with no luck.

I've read Gmail.java and gmail4j both links taken out of this site from this question: Android - How can I find out how many unread email the user has?

But still after having read all of that and a couple of other sites that talked about this particular subject my question remains:

Q: How can I get the Gmail Unread Count?

Sorry if it seams a bit insistent but I clearly lack the knowledge to find this out on my own from the source.

PS: I would like to clarify that I want to do it without having to ask the user for credentials.

Just 2 add some colors to the question let me show you the looks of my app.

alt text

Please note there is a new way of doing this

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's some code snippet. Not sure it works and can't test it. But I hope it will help you to continue the investigation.

public static final class LabelColumns {
    public static final String CANONICAL_NAME = "canonicalName";
    public static final String NAME = "name";
    public static final String NUM_CONVERSATIONS = "numConversations";
    public static final String NUM_UNREAD_CONVERSATIONS = "numUnreadConversations";
}

public void queryLabels(){
    String account="[email protected]";
    Uri LABELS_URI = Uri.parse("content://gmail-ls/labels/");
    Uri ACCOUNT_URI = Uri.withAppendedPath(LABELS_URI, account);
    ContentResolver contentResolver=myActivity.getContentResolver();
    Cursor cursor = contentResolver.query(ACCOUNT_URI, null, null, null, null);

    //iterate over all labels in the account
    if (cursor.moveToFirst()) {
        int unreadColumn = cursor.getColumnIndex(LabelColumns.NUM_UNREAD_CONVERSATIONS);
        int nameColumn = cursor.getColumnIndex(LabelColumns.NAME);
        do {
            String name = cursor.getString(nameColumn);
            String unread = cursor.getString(unreadColumn);//here's the value you need
        } while (cursor.moveToNext());
    }
}

Requires permission

<uses-permission android:name="com.google.android.gm.permission.READ_GMAIL"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...