2010-06-07 87 views
8

Please note there is a new way of doing this

我一直試圖讓沒有運氣未讀的Gmail郵件數量。如何獲得未讀的Gmail郵件數量(在Android)

我讀過Gmail.javagmail4j從這個問題中取出該網站的鏈接都:Android - How can I find out how many unread email the user has?

但還是之後看了這一切,一對夫婦的其他網站的是談到了這個特定主題我的問題依然存在:

問::如何獲取Gmail未讀數?

對不起,如果它接縫有點堅持,但我明顯缺乏知識從我自己的來源找到這個。

PS:我想澄清一下,我想要做到這一點,而不必向用戶索要憑據。

只是2添加一些顏色的問題讓我告訴你我的應用程序的外觀。

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

Please note there is a new way of doing this

回答

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 protected]"; 
    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"/> 
+0

這對我來說就像是沒問題,但是通過該代碼,我得到了這個調試消息: 06-14 17:37:03.451:DEBUG/Gmail(13764):MailProvider.query:content:// gmail-ls/labels/------ @ gmail.com(null,null) 請注意,我的電子郵件地址不是[email protected],但我已將其替換。 還需要正確查詢權限「com.google.android.providers.gmail.permission.READ_GMAIL」 。 – 2010-06-14 20:40:43

+0

在2.2上適合我。返回未讀消息的正確數量。我需要的權限是com.google.android.gm.permission.READ_GMAIL。所有代碼都來自Gmail.java,因此您可以在此處找到更多詳細信息。 – Fedor 2010-06-15 12:00:49

+0

非常感謝,我會在不同的AVD上嘗試它... – 2010-06-15 19:05:34

5

這是多麼我已經看到它在爲awesome窗口管理器簡單的小工具(是的,這就是它的名字:))來完成。原始腳本在這裏:gmail.lua

基本概念是隻使用inbox feed並獲取特殊「未讀」標籤的所有郵件(您將只獲得摘要,而不是全部內容)。 URL是https://mail.google.com/mail/feed/atom/unread,你只需要獲取它(在認證之後,當然),然後解析它。您可以使用某種類型的XML解析器或僅使用簡單的正則表達式(<fullcount>([%d]+)</fullcount>) - 您正在查找的數字始於<fullcount>標記中。

所以,這是一種方法,很簡單,「笨」,但嘿,它的工作原理:D它可能不是最好的解決方案,因爲它需要你取得整個飼料(取決於數量未讀郵件和連接類型/質量,它可能不是一樣快,只是獲取未讀郵件的數量),但像往常一樣,現實生活中的測試應該清除了:)

+0

感謝您花時間回答問題,但問題是: 我想在Android上執行此操作(問題的標題) 而且我需要執行此操作而無需使用用戶憑據。 (問題的正文) 謝謝:) – 2010-06-13 16:14:26

+0

嗯,是的,我注意到它是針對Android的,從鏈接問題來看,似乎沒有「銀彈」類型的解決方案。所以我想你可能想要使用不同的方法,不依賴於Android的軟件,但更通用。正如你指出的那樣,它有缺點,它需要詢問用戶憑證。 – 2010-06-13 18:39:37

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。

+0

你能再詳細一點嗎?正如我在這個問題上所說的:「我明顯缺乏知識來自己找到這個」 我試圖從Gmail.java中採取該方法,但我不能。 我是否必須複製整個代碼才能正常工作? – 2010-06-13 16:09:10

2

有新的方式如何做到這一點。舊的方式不再工作(21.01.2013)。 檢查以下鏈接: Gmail Public Labels API

+0

感謝您的更新,我不會更改所選的答案,因爲答案當時是正確的。 但我非常感謝您的幫助。 – 2013-01-22 00:56:03

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+。

相關問題