Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Primary Email Account of Android phone?

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 play store. I have read this post and implemented that.

If we are using the AccountManager class for geting the email ids, we will get all the email id added in that phone, so that is not possible, and some says to take the first email id that returned by the AccountManager, but that returns the email id that added in the phone for the first time.

ie, suppose I have added [email protected] and linked that with Google Play, later I have added [email protected] and associated this account with play store, right now I am using Play store with this account. If I have wrote code as follows:

    Account[] accountList = AccountManager.get(this).getAccountsByType("com.google");
Log.d("Play store account:" , accountList[0].name);

the expected out put for the statement is [email protected], but I am getting [email protected]

How can I solve this issue?

like image 995
droidev Avatar asked Sep 08 '15 06:09

droidev


People also ask

How do I change the primary account on my Android?

On your Android phone or tablet, go to myaccount.google.com. In the top right, tap your profile photo or name. Sign out. Sign in with the account you want to use.

How do I find my primary email address?

Click your picture or avatar near Gmail's top right corner. View your primary Gmail email address listed under your name. If you have connected Gmail accounts, the current account is listed on top. Your primary Gmail address also appears in the browser's title or tab bar on the desktop.

What is the default email app on Android?

How to Set up Gmail App on (Android devices) If your phone or smart device is an Android, your default email app will most likely be Gmail – but that does not mean you need a Gmail account to use it.


3 Answers

As far as I read, there is no concept of primary email id in android. and there is no way to get e-mail id associated with play store. so what I did is, I have fetched all gmail ids and took the last one, it is not the main email id, but it should be the first added google account in his device. so in normal use cases user won't play with his first added email id. so we can assume it as primary mail id.

like image 148
droidev Avatar answered Oct 12 '22 23:10

droidev


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 userEmailId = getEmailID(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;
}
like image 35
Mohammed Nathar Avatar answered Oct 12 '22 23:10

Mohammed Nathar


The account you have "associated with the Play Store" is just an app preference of the Play app. You can't read that. The user could download/purchase some apps with test and some with test_new.

like image 2
bryan Avatar answered Oct 12 '22 23:10

bryan