get Primary Email Account of android phone

前端 未结 5 1169
粉色の甜心
粉色の甜心 2021-02-19 07:38

I am working on a project, and I have to fill the EditText automatically with the user\'s primary email, I am considering primary email as the email that associated with google

5条回答
  •  再見小時候
    2021-02-19 08:27

    I agree with @driodev. but I have done it with a different approach. just copy and paste the code... I might be answering this question a bit late but I guarantee this will be helpful to many in future. In fact, I think we can get any account id that is used in the android device just by changing the string in getAccount("com.example"). Here is the code.

    String User_EmailId = getEmiailID(getApplicationContext());
    
     private String getEmailID(Context context) {
        AccountManager accountManager = AccountManager.get(context);
        Account account = getAccount(accountManager);
        if (account == null) {
            return null;
        } else {
            return account.name;
        }
    }
    
    private static Account getAccount(AccountManager accountManager) {
        Account[] accounts = accountManager.getAccountsByType("com.google");
        Account account;
        if (accounts.length > 0) {
            account = accounts[0];
        } else {
            account = null;
        }
        return account;
    }    
    

    `

提交回复
热议问题