开发者

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

开发者 https://www.devze.com 2023-01-03 03:07 出处:网络
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.

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 sit开发者_StackOverflow中文版es 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.

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

Please note there is a new way of doing this


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

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


This is how I've seen it done in a simple widget for the awesome window manager (yes, that's its name :)). Original script is here: gmail.lua.

The basic concept is to just use the inbox feed and get all the mails (you'll get just the summaries, not the whole content) for the special 'unread' tag. The URL is https://mail.google.com/mail/feed/atom/unread, you just have to fetch it (after authentication, of course), and then parse it. You can either use some sort of XML parser or just a simple regexp (<fullcount>([%d]+)</fullcount>) - the number you are looking for is at the beginning, in the <fullcount> tag.

So, that's one way of doing it, quite simple and "dumb", but hey, it works :D It might not be the best solution, as it requires you to fetch the whole feed (depending on the number of your unread messages and the type/quality of connection, it might not be as fast as just fetching the number of unread messages), but as usual, real-life testing should clear that up :)


There is new way how to do it. Old way doesn´t work anymore (21.01.2013). Check following link: Gmail Public Labels API


Maybe you can use the Gmail ContentProvider, please see http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/provider/Gmail.java&q=Gmail.java&sa=N&cd=1&ct=rc

There is a method getNumUnreadConversations or you could use a Observer.


static final String AUTHORITY = "com.google.android.gm";
static final String BASE_URI_STRING = "content://" + AUTHORITY;
static final String LABELS_PARAM = "/labels";
static final String ACCOUNT_TYPE_GOOGLE = "com.google";

public static final String NUM_UNREAD_CONVERSATIONS = "numUnreadConversations";
public static final String CANONICAL_NAME = "canonicalName";

public static final String CANONICAL_NAME_INBOX_CATEGORY_PRIMARY = "^sq_ig_i_personal";
static String[] GMAIL_PROJECTION = {
    CANONICAL_NAME, NUM_UNREAD_CONVERSATIONS
    };

public static Uri getLabelsUri(String account) {
    return Uri.parse(BASE_URI_STRING + "/" + account + LABELS_PARAM);
}

static String[] getAllAccountNames(Context context) {
   final Account[] accounts = AccountManager.get(context).getAccountsByType(
           ACCOUNT_TYPE_GOOGLE);
   final String[] accountNames = new String[accounts.length];
   for (int i = 0; i < accounts.length; i++) {
       accountNames[i] = accounts[i].name;
   }
   return accountNames;
}


protected static int getGmail(Context context) {
    ContentResolver cr = context.getContentResolver();
    Cursor cursor =        cr.query(Launcher.getLabelsUri(BadgeUtils.getAllAccountNames(context)[0]),
            GMAIL_PROJECTION,
            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));;
            break;
        }
    }

    cursor.close();


    return count;
}

Hope above code helps. This should work on Android 2.2+.

0

精彩评论

暂无评论...
验证码 换一张
取 消