I am on a project that I need to list all accounts (Gmail, Exchange, etc.) in the users device.
First things first; my code snippet is below;
public class MainActivity extends AppCompatActivity {
public static final int PERMS_REQUEST_CODE = 1;
private static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.GET_ACCOUNTS)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, PERMS_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.GET_ACCOUNTS}, PERMS_REQUEST_CODE);
}
} else {
AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] accounts = manager.getAccounts();
Log.d(LOG_TAG, "Done! List of accounts: " + accounts.length);
}
}
}
It shows below screen, as expected, and I allow it;
But then, the list of accounts retrieved in the line below is empty;
Account[] accounts = manager.getAccounts();
I have an account on the device, by the way;
What I am missing?
Note: I searched the web and questions like this but no way. They point to "lack of permissions" but my app has.