2017-06-13 154 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()尝试实现这一点。 –

相关问题