如何获取未读gmail邮件的数量(在android上)

时间:2010-06-07 19:52:34

标签: android count gmail

Please note there is a new way of doing this

我一直试图获取未读的Gmail邮件数量而没有运气。

我已阅读Gmail.javagmail4j此网站从这个问题中取出的两个链接:Android - How can I find out how many unread email the user has?

但是在阅读了所有这些以及其他一些谈论这个特定主题的网站后,我的问题仍然存在:

:如何获取Gmail未读数?

很抱歉,如果它有点坚持但我显然缺乏知识从源头找到我自己。

PS:我想澄清一点,我想这样做而不必向用户询问凭据。

只需为问题添加一些颜色,让我向您展示我的应用程序的外观。

alt text http://img716.imageshack.us/img716/8818/lookr.png

Please note there is a new way of doing this

5 个答案:

答案 0 :(得分:22)

这是一些代码段。不确定它是否有效且无法测试。但我希望它能帮助你继续调查。

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"/>

答案 1 :(得分:5)

这就是我在awesome窗口管理器的一个简单小部件中看到的方式(是的,这就是它的名字:))。原始脚本在这里:gmail.lua

基本概念是只使用inbox feed并获取特殊“未读”标记的所有邮件(您只会获得摘要,而不是整个内容)。 URL为https://mail.google.com/mail/feed/atom/unread,您只需要获取它(当然是在身份验证之后),然后解析它。您可以使用某种XML解析器,也可以只使用简单的regexp(<fullcount>([%d]+)</fullcount>) - 您要查找的数字位于<fullcount>标记的开头。

所以,这是做到这一点的一种方式,非常简单和“愚蠢”,但是嘿,它有效:D它可能不是最好的解决方案,因为它需要你取整个饲料(取决于你的数量)未读消息和连接的类型/质量,它可能没有只是获取未读消息的数量那么快,但像往常一样,现实生活中的测试应该清除它:)

答案 2 :(得分:2)

有新的方法可以做到这一点。旧的方式不再适用(21.01.2013)。 检查以下链接: Gmail Public Labels API

答案 3 :(得分:0)

也许您可以使用Gmail ContentProvider,请参阅http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/provider/Gmail.java&q=Gmail.java&sa=N&cd=1&ct=rc

有一个getNumUnreadConversations方法,或者你可以使用Observer。

答案 4 :(得分:0)

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

希望上面的代码有帮助。这适用于Android 2.2 +。

相关问题