7

I know how to find accounts (which will include gmail email ids of user) and how to filter gmail account.

AccountManager am = AccountManager.get(context);
        Account[] accounts = am.getAccounts();
        ArrayList<String> googleAccounts = new ArrayList<String>();
        for (Account ac : accounts) {
            String acname = ac.name;
            String actype = ac.type;
            //add only google accounts
            if(ac.type.equals("com.google")) {
                googleAccounts.add(ac.name);
            }
            Log.d(TAG, "accountInfo: " + acname + ":" + actype);
        }
        return googleAccounts;

What I wish to know is how to find the gmail account associated with Android Market? If you will try to delete a gmail account from Android phone you will get this message enter image description here

but if you try to delete a gmail account associated with Android Market you will get the following message (if you press remove account in the earlier message).

enter image description here

Thanks for the help.

Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
  • You mean the primary email account? check this http://stackoverflow.com/questions/2112965/how-to-get-the-android-devices-primary-e-mail-address – Ron May 19 '12 at 07:52
  • 1
    @userSeven7s The answer only talks about getting the accounts on the device, not the one associated with the Android Market. As you can see there is a difference. – Gaurav Agarwal May 19 '12 at 09:30
  • Isn't it the primary account that is associated with the market app? – Ron May 19 '12 at 09:34
  • 2
    @userSeven7s As far I know there is nothing like "primary account" defined in Android Documentation. – Gaurav Agarwal May 19 '12 at 09:37
  • @GauravAgarwal Does your code work for android version after/from marshmellow? – Eswar Oct 12 '18 at 06:52

3 Answers3

3

From what I have found, the first google account added in the device is the primary google address which is associated with android market. Also, we get google accounts in the order it was added in the device. So, accounts[0] will be the first email-Id added.

String marketAssociatedEmailId = "";
Account[] accounts = AccountManager.get(context).getAccountsByType("com.google");
if(accounts.length > 0) {
  marketAssociatedEmailId = accounts[0].name;
}

Again, I am not sure about this. You have to test it yourself with various scenarios.

Hemanth
  • 2,717
  • 2
  • 21
  • 29
  • 1
    I can change my email address on the Market and buy apps in a different account than my primary (the one I first logged-in on the device. It would be good to know which account bought/downloaded your app. So we can control expiration and stuff alike. – Dpedrinha Nov 27 '14 at 19:52
2

The dialog

enter image description here

is caused by the implementation of AbstractAccountAuthenticator#getAccountRemovalAllowed in the Google account authenticator.

Since calling implementations of AbstractAccountAuthenticator directly is prevented by the system-only permission android.permission.ACCOUNT_MANAGER you're going to have a hard time finding this out on your own.

Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
Jens
  • 16,853
  • 4
  • 55
  • 52
  • You are one heck of a guy. Read you comment here also http://stackoverflow.com/questions/9801304/android-contentprovider-calls-bursts-of-setnotificationuri-to-cursoradapter-wh. Just a quick question what do you mean by _system-only permission._ – Gaurav Agarwal May 22 '12 at 10:23
  • It means that in order to obtain the required permission, `android.permission.ACCOUNT_MANAGER`, your application needs to be signed with the same certificate as the system applications - effectively preventing home-grown apps from calling it unless you've rooted your device. – Jens May 22 '12 at 10:45
  • But I could the permission `android.permission.ACCOUNT_MANAGER` in `Uses Permission` in Android Manifest. – Gaurav Agarwal May 22 '12 at 11:02
  • Sure, you can add whatever permission you want there - your application will however not be granted the permission, i.e. if you were to jump through all the hoops (i.e. copying the necessary AIDL files & creating the stubs for the `IAccountAuthenticator` API) it would still reject your calls – Jens May 22 '12 at 11:13
0

The 'association' will be managed by the Android Market/Google Play application, and I don't think it has any public API's that tell you what accounts it uses. Additionally, in recent versions, it can use multiple accounts, so potentially, any Google account on the phone can be used with the Market/Play Store app.

Why exactly are you trying to do this? To be able to delete accounts 'safely'? The user will get an appropriate error message, so they should be able to decide whether to remove the account or not.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • 3
    I want to register the user in my app with the gmail account associated with the Market. Gmail associated with Market ensures that it is not deleted or changed frequently. – Gaurav Agarwal May 18 '12 at 07:59
  • 4
    OK, I see. I don't think it's possible to find out reliably though. Your best bet is probably to use the first listed Google account. It is setup when activating the device and is generally not removed. – Nikolay Elenkov May 18 '12 at 08:05
  • I guess you are right..:). Let us wait for few days and see if someone has some way around. – Gaurav Agarwal May 18 '12 at 08:07