Can't get default account in OREO

本秂侑毒 提交于 2019-12-02 01:19:00

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.

See the documentation: 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.

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

A sample code is given below:

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

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.

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

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

Happy Coding!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!