本文介绍了Android 8.0 Oreo - 帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我需要知道是否有任何Google帐户或任何三星帐户。

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

Android 7很容易获得这些信息喜欢:

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.

编辑:查看有关此主题的官方信息:
在Android 8.0(API级别26)中,应用程序无法再使用除非身份验证者拥有帐户或用户授予该访问权限,否则可以访问用户帐户。 GET_ACCOUNTS权限已不再足够。要授予对帐户的访问权限,应用应使用AccountManager.newChooseAccountIntent()或特定于身份验证器的方法。访问帐户后,应用程序可以调用AccountManager.getAccounts()来访问它们。
Android 8.0弃用了LOGIN_ACCOUNTS_CHANGED_ACTION。应该使用addOnAccountsUpdatedListener()代替应用程序在运行时获取有关帐户的更新。
有关为帐户访问和可发现性添加的新API和方法的信息,请参阅本文档新API部分中的帐户访问和可发现性

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.

我发现信息声称现在访问帐户的唯一方法是使用 AccountPicker 像这样:

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);

但这确实可以解决我的问题。需要说明的是,我只需要知道某个类型的帐户是否存在(Google,三星......)我不需要知道多少帐户,也不需要帐户信息。

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.

推荐答案

正如您所说,如果用户未授予您许可,则无法读取其他帐户。现在,权限不仅提供了运行时权限,而且还提供了帐户选择器,即只有在您调用帐户选择器后用户选择了帐户时,您的应用才能看到该帐户。这个新限制正是为了避免您尝试做的事情:阅读所有用户帐户。您的问题没有解决方案,您唯一能做的就是向用户展示选择器并让他选择所有帐户,而不是最佳用户体验。

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.

编辑:从Google Play Services 11.6开始,现在有一种新方法 requestGoogleAccountsAccess()可以获取所有Google帐户。

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

这篇关于Android 8.0 Oreo - 帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:31