2

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 Answers2

1

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

Noban Hasan
  • 593
  • 1
  • 7
  • 21
  • I check thos programm for android 6 - work ok. Thank you, really api 26 no work. What is way get account list? Only get, don need remove or use. – Prostakov Alexey Jun 25 '20 at 11:39
0

add read_contacts permission , get_account is not sufficient.

Bhushan S
  • 21
  • 5