Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - What to use in place of AccountPicker.newChooseAccountIntent because its deprecated

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);
like image 882
Pankaj Avatar asked Oct 22 '25 03:10

Pankaj


1 Answers

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);
    }
like image 178
remain4life Avatar answered Oct 24 '25 19:10

remain4life