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 test@gmail.com and linked that with google play, later I have added test_new@gmail.com 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 test_new@gmail.com, but I am getting test@gmail.com
is there any way to solve this issue ?
please help me contributing your ideas.
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.
`
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.
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.
Whenever user assign email_id then android create a calendar with Email.So if above solution not turning out for you then you can try this as hack. Find Email associated with Calendar. Hope this will help.
Some additions to @MohammedNathar's answer. On Android 6.0 and above, don't forget to request permissions, because this versions are some paranoic:
Manifest:
Request:
And in activity
Or you can do it with one "if" if you want.