2017-06-13 150 views
0

您好,我正在嘗試從我的Gmail帳戶中查找未讀郵件的數量,因爲我在Google中搜索了很多,但我沒有得到任何工作解決方案的 ,最後我發現了一個從下面的鏈接文件我也跟着相同的過程,但它始終返回未讀郵件數爲0,但在Gmail帳戶有2個未讀郵件如何從Gmail帳戶中獲取未讀郵件數

http://android-developers.blogspot.in/2012/04/gmail-public-labels-api.html

可有一個人幫我,因爲3天請我在等待正確的解決方案

public static int getGmailCount(Context context) { 

     ContentResolver cr = context.getContentResolver(); 
     Cursor cursor = cr.query(GmailContract.Labels.getLabelsUri("[email protected]"), 
       null, 
       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)); 
       System.out.println("count is====>" + count); 
       break; 
      } 
     } 
     cursor.close(); 
     return count; 
    } 
+0

查看此問題的答案。我認爲「threadsUnread」 是你需要的.https://stackoverflow.com/questions/44499338/get-the-unread-mail-count-gmail-in-android –

+0

有沒有從你的鏈接提供的解決方案 - > https://stackoverflow.com/questions/44499338/get-the-unread-mail-count-gmail-in-android – Krish

+0

如果我想分享我的示例代碼給你,請建議我 – Krish

回答

1

我從來沒有試過這個,但你可以試試這個。

public class MainActivity extends AppCompatActivity { 
    AccountManager accountManager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     accountManager = AccountManager.get(this); 
     Account account= getAccount(accountManager); 
     Log.d("MainActivity","UnreadCount-----> "+getUnreadCount(account.name)); 
    } 
    public Account getAccount(AccountManager accountManager) { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return null; 
     } 
     Account[] accounts = accountManager.getAccountsByType("com.google"); 
     Account account; 
     if (accounts.length > 0) { 
      account = accounts[0]; 
     } else { 
      account = null; 
     } 
     return account; 
    } 
    public int getUnreadCount(String accountName) { 
     Cursor cursor = getContentResolver().query(
       GmailContract.Labels.getLabelsUri(accountName), 
       UnreadQuery.PROJECTION, null, null, null 
     ); 
     try { 
      if (cursor == null || cursor.isAfterLast()) { 
       return 0; 
      } 

      int unread = 0; 
      while (cursor.moveToNext()) { 
       String canonicalName = cursor.getString(UnreadQuery.CANONICAL_NAME); 
       int unreadInLabel = cursor.getInt(UnreadQuery.NUM_UNREAD_CONVERSATIONS); 
       if (GmailContract.Labels.LabelCanonicalNames.CANONICAL_NAME_INBOX_CATEGORY_PRIMARY.equals(canonicalName)) { 
        unread = Math.max(unread, unreadInLabel); 
       } 
      } 
      return unread; 
     } finally { 
      if (cursor != null) { 
       cursor.close(); 
      } 
     } 
    } 

    private interface UnreadQuery { 
     String[] PROJECTION = { 
       GmailContract.Labels.NUM_UNREAD_CONVERSATIONS, 
       GmailContract.Labels.CANONICAL_NAME, 
     }; 
     int NUM_UNREAD_CONVERSATIONS = 0; 
     int CANONICAL_NAME = 1; 
    } 
} 
+0

我會讓嘗試並告訴結果 – Krish

+0

沒有它不工作,我的意思是控制器不是進入裏面,如果循環,你可以請你的系統一次,並請求更新我 – Krish

+0

好的。我沒有時間來測試這個。但我只是使用這個https://github.com/johnsto/mailcircle項目,並能夠打印未讀的計數。 https://github.com/johnsto/mailcircle/blob/master/app/src/main/java/uk/co/johnsto/mailcircle/NotificationService.java - > getUnreadCount()嘗試實現這一點。 –

相關問題