无法在OREO中获得默认帐户 [英] Can't get default account in OREO

查看:84
本文介绍了无法在OREO中获得默认帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android Oreo中,AccountManager.getAccountsByType("com.google");返回null.

In Android Oreo, AccountManager.getAccountsByType("com.google"); returns null.

它,在低于Android 8的版本中都可以正常工作.

Its, working fine in below Android 8 versions.

下面是我的代码:

private static Account getAccount(AccountManager accManager) {
    Account[] accounts = accManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
        account = accounts[0];
    } else {
        account = null;
    }
    return account;
}

谢谢.

推荐答案

根据Android的更新,从Oreo开始,我们无法使用AccountManager.getAccountsByType获取在用户设备上配置的Google帐户列表,因为他们已经更新了Google登录功能.这项新功能将提示用户选择该帐户,并且该帐户仅对我们的应用可见.

As per Android's update, from Oreo onwards we can not use AccountManager.getAccountsByType to get the list of google accounts configured on user's device, as they have updated the Google SignIn features. The new feature will prompt the user to select the account and that account will be only visible to our app.

请参阅文档: https://developer.android .com/about/versions/oreo/android-8.0-changes#aaad

如果您仍然想继续使用向用户显示所有帐户的旧方法,则需要通过执行以下步骤来获得用户的额外同意.

If you still want to continue with the old approach of showing all the account's to users, you need to get an extra consent from user by doing below procedures.

您可以使用GoogleAuthUtil.requestGoogleAccountsAccess获取Google帐户列表.

You can use GoogleAuthUtil.requestGoogleAccountsAccess to get the list of Google accounts.

下面给出了示例代码:

new Thread(() -> {
            try {
                GoogleAuthUtil.requestGoogleAccountsAccess(getApplicationContext());
            } catch (Exception e) {
                if (e instanceof UserRecoverableAuthException) {
                    startActivityForResult(((UserRecoverableAuthException) e).getIntent(),
                            REQ_CODE_PERMISSION_GET_GOOGLE_ACCOUNTS);
                } else {
                    Log.e("SignIn", "Exception in getting google accounts" + e);
                }
            }

        }).start();

这将创建一个活动,提示用户接受同意以允许Google Play服务访问在设备上配置的Google帐户列表.

This will create an activity to prompt user to accept the consent to allow Google Play Service to access the list of google accounts configured on the device.

然后您可以在活动中覆盖onActivityResult()功能以在此之后继续.

You can then override onActivityResult() function on your activity to continue after.

然后,您可以像以前一样使用AccountManager.getAccountsByType获取Google帐户列表.

Then you can use AccountManager.getAccountsByType to get the list of google accounts as you done before.

快乐编码!

这篇关于无法在OREO中获得默认帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆