How to get the Android device's primary e-mail address

后端 未结 12 2303
借酒劲吻你
借酒劲吻你 2020-11-21 13:30

How do you get the Android\'s primary e-mail address (or a list of e-mail addresses)?

It\'s my understanding that on OS 2.0+ there\'s support for multiple e-mail add

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 14:06

    Add this single line in manifest (for permission)

    Then paste this code in your activity

    private ArrayList getPrimaryMailId() {
        ArrayList accountsList = new ArrayList();
        try {
            Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
            for (Account account : accounts) {
                accountsList.add(account.name);
                Log.e("GetPrimaryMailId ", account.name);
            }
        } catch (Exception e) {
            Log.e("GetPrimaryMailId", " Exception : " + e);
        }
        return accountsList;
    }
    

提交回复
热议问题