這是一些代碼片段。不確定它是否有效,無法測試。但我希望它能幫助你繼續調查。
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"/>
這對我來說就像是沒問題,但是通過該代碼,我得到了這個調試消息: 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
在2.2上適合我。返回未讀消息的正確數量。我需要的權限是com.google.android.gm.permission.READ_GMAIL。所有代碼都來自Gmail.java,因此您可以在此處找到更多詳細信息。 – Fedor 2010-06-15 12:00:49
非常感謝,我會在不同的AVD上嘗試它... – 2010-06-15 19:05:34