Reputation: 137
I want get account list in Android 8.1. I make:
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
// 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:
Upvotes: 2
Views: 2145
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
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