Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 8.0 Oreo - Accounts

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.

like image 606
Steeve Favre Avatar asked Sep 29 '17 19:09

Steeve Favre


People also ask

Is Android 8.0 Oreo still supported?

Google no longer supports Android 8.0 Oreo.

What is the name of Android 8.1 0?

Android Oreo (Go edition) Starting with Android 8.1, we're making Android a great platform for entry-level devices. Features in the Android Oreo (Go edition) configuration include: Memory optimizations. Improved memory usage across the platform to ensure that apps can run efficiently on devices with 1GB or less RAM.


2 Answers

Using "android.permission.READ_CONTACTS" permission, and

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

working again in android Oreo

like image 132
nguyenvangiangbn Avatar answered Sep 23 '22 09:09

nguyenvangiangbn


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.

like image 27
greywolf82 Avatar answered Sep 23 '22 09:09

greywolf82