如何获得的未读的Gmail邮件的数量(在Android)

如何获得的未读的Gmail邮件的数量(在Android)

问题描述:

我一直在试图让未读的Gmail邮件的数量,没有运气。

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

我读过 Gmail.java 和 gmail4j 两个链接取出到这个问题,这个网站:Android - ?我如何才能知道有多少未读邮件的用户

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:

问::我怎样​​才能获得Gmail的未读​​邮件数

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:我想澄清的是,我想这样做,而不必要求用户提供凭据。

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

仅有2添加一些颜色的问题让我告诉你我的应用程序的外观。

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

下面是一些code段。不知道它的工作原理,并不能对其进行测试。但我希望它会帮助你继续进行调查。

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@company.com";
    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());
    }
}

要求允许

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