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

前端 未结 5 617
忘掉有多难
忘掉有多难 2020-11-27 02:31

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 l

5条回答
  •  无人及你
    2020-11-27 03:32

    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());
        }
    }
    

    Requires permission

    
    

提交回复
热议问题