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

后端 未结 12 2306
借酒劲吻你
借酒劲吻你 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:04

    Use this method:

     public String getUserEmail() {
        AccountManager manager = AccountManager.get(App.getInstance());
        Account[] accounts = manager.getAccountsByType("com.google");
        List possibleEmails = new LinkedList<>();
        for (Account account : accounts) {
            possibleEmails.add(account.name);
        }
        if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
            return possibleEmails.get(0);
        }
        return "";
    }
    

    Note that this requires the GET_ACCOUNTS permission:

    
    

    Then:

    editTextEmailAddress.setText(getUserEmail());
    

提交回复
热议问题