Search code examples
androidaccountaccountmanager

Get all types of accounts Android API 29


I'm new to android and trying to get the list of accounts that an android user has registered in his/her device. I've used android's account manager to do this as follows:

public static int count;
count=0;
JSONArray jsonArr = new JSONArray();
JSONObject emailObj = new JSONObject();
Account[] accounts = AccountManager.get(mContext).getAccounts();
        //Account[] accounts = AccountManager.get(mContext).getAccountsByType(null);
        for (Account account : accounts) {
            //possibleEmail += " "+account.name+" : "+account.type+" \n";

            JSONObject elem = new JSONObject();
            elem.put("name", account.name);
            elem.put("type", account.type);
            jsonArr.put(elem);
            //possibleEmail += " "+account.name+" \n";
            count++;
            emailObj.put("list",jsonArr);

        }

The code above works as desired for API level 23 for instance. I'm getting list of accounts as follows:

[{"name":"[email protected]","type":"com.google"},{"name":"[email protected]","type":"com.google"},{"name":"[email protected]","type":"com.google"},{"name":"handler","type":"com.twitter.android.auth.login"},{"name":"Facebook","type":"com.facebook.auth.login"},{"name":"LinkedIn","type":"com.linkedin.android"}]

Notice that I can detect if user is logged in on third party apps. I do have linkedin, fb, and twitter installed and I'm logged in on those platforms from my device. I just need to capture this (as shown in the list above), not the actual account names.

The point is that when I use the same code for Android API level 29, it does not return third party accounts like com.twitter.android.auth.login, com.facebook.auth.login, and "com.linkedin.android. However, I CAN see the gmail accounts logged in on the device. How can I do to retrieve this information in recent android versions?

I also should say that I'm running the newest API 29 from an android emulator and don't know if this is the reason. I installed linkedin, twitter, and fb in the emulator and am logged in there too.

I just need to get the same result as the one shown above but for new android versions.


Solution

  • Generally, no, you cannot get programmatic access to other apps' accounts on newer devices.

    As per the API 26 Behavior changes:

    In Android 8.0 (API level 26), apps can no longer get access to user accounts unless the authenticator owns the accounts or the user grants that access. The GET_ACCOUNTS permission is no longer sufficient. To be granted access to an account, apps should either use AccountManager.newChooseAccountIntent() or an authenticator-specific method. After getting access to accounts, an app can can call AccountManager.getAccounts() to access them.

    So you should specifically ask the user to select an account using the newChooseAccountIntent() (an API available back to API 14) so that the user can give you access to specifically the account they want to make available to your app.

    As mentioned on that same page, there are new AccountManager APIs that allow specific apps that own an account to control whether to hide accounts from, or reveal accounts to, any other app. This may explain why you have access to some subset of what you were able to get before.