Request p" /> Request p" /> Request p"/>

How get account list in android?

1.7k views Asked by At

I want get account list in Android 8.1. I make:

  1. Add permition in manifect:
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
  1. Request permition from user.
  2. Check permition and get account list:
        // account
        Context mContext = App.Companion.getInstance();
        int permissionStatus = ContextCompat.checkSelfPermission(mContext, Manifest.permission.GET_ACCOUNTS);
        if (permissionStatus == PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "hasAccount: Have permission" );
            AccountManager accountManager = AccountManager.get(mContext);
            Account[] accounts = accountManager.getAccounts();
            if (accounts.length > 0) {
                result.put("hasAccount", true);
            } else {
                result.put("hasAccount", false);
            }
        }
        else {
            Log.e(TAG, "hasAccount: Have not permission!" );
            result.put("hasAccount", true);
        }

In log: D/SecureService: hasAccount: Have permission and in accounts = []. Why android return entry account list? I have account in phone:

enter image description here

2

There are 2 answers

1
Noban Hasan On

From Android 8.0, GET_ACCOUNTS is no longer sufficient to access the Accounts on device.

Based on the documentation:

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 call AccountManager.getAccounts() to access them.

You can check this link for usage of AccountManager.newChooseAccountIntent()

0
Bhushan S On

add read_contacts permission , get_account is not sufficient.