Prostakov Alexey
Prostakov Alexey

Reputation: 137

How get account list in android?

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

Upvotes: 2

Views: 2145

Answers (3)

dot_cr2
dot_cr2

Reputation: 127

Also suffered from this problem, no accounts, nothing ever printed when testing. But what finally worked for me was:

Using getSystemService() instead of .get()

Adding android.permission.READ_CONTACTS permission together with android.permission.GET_ACCOUNTS permission

(Also have to request GET_ACCOUNTS permission programatically)

AccountManager manager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account[] list = manager.getAccounts();

    for(Account account: list) {
        System.out.println("Account: " + account.name);
    }

Upvotes: 0

Bhushan S
Bhushan S

Reputation: 21

add read_contacts permission , get_account is not sufficient.

Upvotes: 0

Noban Hasan
Noban Hasan

Reputation: 713

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

Upvotes: 2

Related Questions