How to find Gmail account associated with Android

2020-02-07 01:56发布

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.

3条回答
▲ chillily
2楼-- · 2020-02-07 02:15

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.

查看更多
够拽才男人
3楼-- · 2020-02-07 02:26

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.

查看更多
Luminary・发光体
4楼-- · 2020-02-07 02:32

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.

查看更多
登录 后发表回答