I am working on a project where I have to show the account chooser so that a user can select an email account which is stored in his device. Problem is I have got AccountPicker.newChooseAccountIntent which is deprecated.
Is there any alternate way to show the account chooser instead of getting the email manually, and showing it in a custom view
Right now I am using:
Intent googlePicker = AccountPicker.newChooseAccountIntent(null, null,
new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, true, null, null, null, null);
startActivityForResult(googlePicker, PICK_ACCOUNT_REQUEST);
Maybe it will be helpful to someone, in 2020 use this according to the docs:
Intent intent =
AccountPicker.newChooseAccountIntent(
new AccountChooserOptions.Builder()
.setAllowableAccountsTypes(Arrays.asList("com.google"))
.build());
startActivityForResult(intent, SOME_REQUEST_CODE);
Also you can use AccountManager:
Intent intent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
intent = AccountManager.newChooseAccountIntent(null, null,
new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, null, null, null, null);
} else {
intent = AccountManager.newChooseAccountIntent(null, null,
new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
}
startActivityForResult(intent, SOME_REQUEST_CODE);
You can get selected email from authAccount extra:
protected void onActivityResult(final int requestCode, final int resultCode,
final Intent data) {
if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
// Do what you need with email
}
super.onActivityResult(requestCode, resultCode, data);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With