【问题标题】:How to get Number of Unread mails count from Gmail account如何从 Gmail 帐户获取未读邮件数
【发布时间】:2017-11-14 19:57:50
【问题描述】:

您好,我正在尝试从我的 Gmail 帐户中查找未读邮件的数量,为此我在 Google 中搜索了很多,但我没有得到 任何可行的解决方案,最后我从下面的链接中找到了一个文档,我遵循了相同的过程,但它总是返回未读邮件计数为 0,但在 Gmail 帐户中有 2 条未读邮件

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("ensisinfo102@gmail.com"),
                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;
    }

【问题讨论】:

标签: android gmail-api


【解决方案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;
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 2014-10-16
  • 2014-09-08
  • 2018-05-18
  • 2014-04-19
  • 2013-11-17
  • 1970-01-01
相关资源
最近更新 更多