AccountManager getAccount is acquired as null in Android O 8.0?

Keywords: Android Google

problem

  AccountManager accountManager = AccountManager.get(this);
  Account[] accounts = accountManager.getAccounts();

The above code works well before Android 8.0 (API 26) and can get account information. However, it is not available on the latest version 8.0, returning accounts as null.

In addition, Why do I get null from retrieving the user's gmail?
It's a similar problem.

Reason

It turned out that this was a behavioral change in Android 8.0.

Account access and discoverability
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

Unless the authenticator has a user account or the user grants access rights, the application will no longer be able to access the user account. GET_ACCOUNTS permission alone is not enough to access user accounts. To gain access to accounts, applications should use AccountManager.newChooseAccountIntent() or authentication-specific functions. After gaining access to the account, the application can call AccountManager.getAccounts() to access the account.

Android 8.0 has discarded LOGIN_ACCOUNTS_CHANGED_ACTION. Instead, the application should use addOnAccounts UpdatedListener () to obtain account update information at runtime.

For information on new APIs and additional account access and detectability functions, see Account Access and detectability in the New API section of this document.

In addition, you can refer to the following article
android 8.0-AccountManager Behavior Change

Solve

According to the document,

To gain access to accounts, applications should use AccountManager.newChooseAccountIntent() or authentication-specific functions. After gaining access to the account, the application can call AccountManager.getAccounts() to access the account.

Therefore

Intent googlePicker = AccountManager.newChooseAccountIntent(null, null,
                new String[] { "com.google"}, true, null, null, null, null);
startActivityForResult(googlePicker, PICK_ACCOUNT_REQUEST);
    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        if (requestCode == PICK_ACCOUNT_REQUEST && resultCode == RESULT_OK) {
            String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            Log.d(TAG, "Account Name=" + accountName);
            String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
            Log.d(TAG, "Account type=" + accountType);

            AccountManager accountManager = AccountManager.get(this);
            Account[] accounts = accountManager.getAccounts();
            for (Account a : accounts) {
                Log.d(TAG, "type--- " + a.type + " ---- name---- " + a.name);
            }
        }
    }

The problem can be solved.

Posted by adguru on Fri, 11 Jan 2019 23:33:10 -0800