How can I get Primary Email Account of Android phone?

15,694

Solution 1

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.

Solution 2

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;
}

Solution 3

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.

Share:
15,694

Related videos on Youtube

droidev
Author by

droidev

Professionally Android developer. Love to work on MEAN Stack.

Updated on October 14, 2022

Comments

  • droidev
    droidev over 1 year

    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?

  • DaveNOTDavid
    DaveNOTDavid about 8 years
    I tried this, but why is it that I keep getting null for my Android device when I print it? My device is running only on one Google account, gmail.
  • Mohammed Nathar
    Mohammed Nathar about 8 years
    did you add permission in manifest !! did you check for typo mistake say for ex in the above "com.google" i was having it as "com.gogle" for a wile i could not figure out where the problem was.. i would be helpful if you could provide your code snippet
  • Mohammed Nathar
    Mohammed Nathar about 8 years
    this should be the permission in the manifest <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  • DaveNOTDavid
    DaveNOTDavid about 8 years
    Yup, I already included the permission into my manifest. Here's my code snippet: stackoverflow.com/questions/36750402/…
  • DaveNOTDavid
    DaveNOTDavid about 8 years
    Mine's 6.0, and hers is 4.4... Could that be the reason why?
  • Mohammed Nathar
    Mohammed Nathar about 8 years
    i assume that this code works fine on version 4.4 and not on 6.... i think google has made changes in account manager .. now this makes me check my app again for compatibility purpose... please vote my answer if you found it usefull
  • Mohammed Nathar
    Mohammed Nathar about 8 years
    dpark@ did you figure out why you were getting null for email for your android device (version 6.0)
  • Vishnu V
    Vishnu V almost 8 years
    Me too have this problem. This need to Read Contacts Permission also.
  • Neo
    Neo over 5 years
    It must be in comment.. not an answer.!