Android 8.0 Oreo - Accounts
Asked Answered
B

3

7

In my app, I need to known if there is any Google account or any Samsung account.

Up to Android 7 it was easy to get this information with something like:

    Account[] accounts = AccountManager.get(getContext())
.getAccountsByType("com.google")

But with the event of Oreo this does not work anymore.

EDIT: see official information on this subject: 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. Android 8.0 deprecates LOGIN_ACCOUNTS_CHANGED_ACTION. Apps should instead use addOnAccountsUpdatedListener() to get updates about accounts during runtime. For information about new APIs and methods added for account access and discoverability, see Account Access and Discoverability in the New APIs section of this document

I spent half a day to find a solution to my need, without success.

I've found information claiming that now the only way to access to accounts is to use AccountPicker like this:

AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"},true, null, null, null, null);

But this does respond to my problem. To be clear I only need to know if an account exists for a certain type (Google, Samsung...) I do not need to know how much if so and do not need accounts information.

Brigittebriley answered 29/9, 2017 at 19:54 Comment(6)
what do you mean by "this does not work anymore" ? can you provide some code, log or stack trace, whatever seems relevant to you, in order for the community to be able to help you ?Tichonn
I mean the getAccount() method do not return account data anymore execept if for accounts which are linked to the App.Brigittebriley
Here is more details from official documentation: 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.Brigittebriley
@Steeve Favre can u share the link regarding that and will u please tell how can this be resolved.Kaiserdom
Please see my answer. This is how you get it workingOrthotropous
A detailed approach is mentioned over here: https://mcmap.net/q/1479539/-can-39-t-get-default-account-in-oreoStature
V
6

Using "android.permission.READ_CONTACTS" permission, and

    Account[] accounts = AccountManager.get(getContext())
.getAccountsByType("com.google") 

working again in android Oreo

Vaish answered 2/10, 2018 at 10:20 Comment(0)
P
5

As you already said, there's no way to read other accounts if the user didn't give you the permission to do so. The permission now is provided not only with the run-time permission but even with the account picker, i.e. an account is visible to your app only if the user selected the account after you called the account picker. This new restriction is exactly to avoid what you are trying to do: read all user accounts. There's no solution to your problem, the only thing you can do is to present the picker to the user and let him select all the accounts, not the best user experience however.

Edit: starting from Google Play Services 11.6 there's now a new method requestGoogleAccountsAccess() to get all Google accounts.

Pepsinate answered 1/10, 2017 at 14:22 Comment(4)
As I said, I do NOT want to read any information about accounts. I just need to know if one exists for a certain type that's all. But I guess that you are right that even for that there is no solution.Brigittebriley
I know this is late, so I'll post only for future readers. Learning about whether or not an account exists is still information you need access to obtain. IE: the user may not want you to know what accounts exist as the existence of an account is in itself private information.Quattlebaum
@AaronTHarris will u please tell how this problem get resolved how can I get the name of user from linked google Account.Kaiserdom
Please see my answer. This is how you get it workingOrthotropous
O
1

To get the installed google accounts on a device running Oreo+ (8+) with this code

 Account[] accounts = AccountManager.get(getContext()).getAccountsByType("com.google")

You need to first call

https://developers.google.com/android/reference/com/google/android/gms/auth/GoogleAuthUtil.html#requestGoogleAccountsAccess(android.content.Context)

Please add the following dependency first

com.google.android.gms:play-services-auth:16.0.0

The call requestGoogleAccountsAccess() throws an exception which you can cast (after checking) to UserRecoverableAuthException and get an intent from it to start with startActivityForResult

Here is some example code, working on Android Oreo

// call this on a background thread!
private void requestGoogleAccountAccess() throws Exception
{
    googleAccountAccessGranted = GoogleAuthUtil.requestGoogleAccountsAccess(this);
    Log.i(TAG, "googleAccountAccessGranted: " + googleAccountAccessGranted);
}

// exception handler after calling method above
private void handleAuthResult(Throwable e)
{
    if (e instanceof UserRecoverableAuthException)
    {
        UserRecoverableAuthException authException = (UserRecoverableAuthException) e;
        startActivityForResult(authException.getIntent(), AUTH_PERMISSION_REQUEST);
    }
    else
    {
        Log.e(TAG, "Cannot request Google Account Access", e);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == AUTH_PERMISSION_REQUEST)
    {
        Log.i(TAG, "Google Auth Permission Result");

        if (resultCode == Activity.RESULT_CANCELED)
        {
            Log.w(TAG, "User Cancelled Play Services Auth Request.")
        }
        else if (resultCode == Activity.RESULT_OK)
        {
            Log.d(TAG, "User accepted Play Services Auth Request.");
            // call the following line again on a background thread. the call now returns a boolean instead of throwing an exception
            //  googleAccountAccessGranted = GoogleAuthUtil.requestGoogleAccountsAccess(this);
        }
    }
}

It's a bit strange why Google decided themselves for this "architecture". Why not return a Task, etc. But this is how you get it working.

Of course this code needs proper exception handling which I left out for readability.

Orthotropous answered 4/10, 2018 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.