Get the unread mail count gmail in Android

有些话、适合烂在心里 提交于 2019-12-24 06:51:44

问题


I want to get an int with the number of unread emails in the accounts of the device. I have seen that there is a new way to do this using the "Gmail Labels Public API"

http://android-developers.blogspot.in/2012/04/gmail-public-labels-api.html

I have read the documentation and downloaded the sample application and it really works. But I have two problems: (

My intention is to get an int with the number of unread conversations, i try this:

 public static int getUnreadGmailCount(Context context) {

    ContentResolver cr = context.getContentResolver();
    Cursor cursor = cr.query(GmailContract.Labels.getLabelsUri("ensisinfo102@gmail.com"),
            null,
            null, null,
            null);
    if (cursor == null || cursor.isAfterLast()) {
        Log.d(TAG, "No Gmail inbox information found for account.");
        if (cursor != null) {
            cursor.close();
        }
        return 0;
    }
    int count = 0;
    while (cursor.moveToNext()) {
        if (CANONICAL_NAME_INBOX_CATEGORY_PRIMARY.equals(cursor.getString(cursor.getColumnIndex(CANONICAL_NAME)))) {
            count = cursor.getInt(cursor.getColumnIndex(NUM_UNREAD_CONVERSATIONS));
            System.out.println("count is====>"+count);
            break;
        }
    }
    cursor.close();
    return count;
}

but not works, always returns "0",But in gmail i have 3 unread messages

really appreciate any help

thanks and regards


回答1:


You can get a label and check the messagesUnread. The INBOX label is probably what you want:

Request

GET https://www.googleapis.com/gmail/v1/users/me/labels/INBOX?access_token={ACCESS_TOKEN}

Response

{
 "id": "INBOX",
 "name": "INBOX",
 "messageListVisibility": "hide",
 "labelListVisibility": "labelShow",
 "type": "system",
 "messagesTotal": 4527,
 "messagesUnread": 4498,
 "threadsTotal": 4168,
 "threadsUnread": 4154
}



回答2:


Please read carefully this document and also check this one



来源:https://stackoverflow.com/questions/44499338/get-the-unread-mail-count-gmail-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!