Pick an email using AccountPicker.newChooseAccountIntent
Asked Answered
A

3

25

I'm trying to let the user pick an Email account using the following code:

Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"},
                            false, null, null, null, null);
                    startActivityForResult(intent, 23);

This code works great but if the user doesn't have a Gmail account but Yahoo, Hotmail, etc.. How can I show all Email accounts by changing the third parameter:

new String[]{"com.google"}

Thank you very much

Anaximander answered 4/3, 2014 at 14:11 Comment(0)
M
4

It's 2019, and the code does not seem to work anymore. To get all accounts shown in the picker (using Xamarin Android), instead of

Android.Gms.Common.AccountPicker.NewChooseAccountIntent(null, null, 
null, false, null, null, null, null);

you have to use

Android.Accounts.AccountManager.NewChooseAccountIntent(null,null,null,null,null,null,null)
Munafo answered 13/12, 2019 at 9:58 Comment(2)
The question is specifically about AccountPicker.newChooseAccountIntent() class/method.Stockmon
You may be right... (why was it selected as an answer?)Munafo
S
25

According to the docs, the third parameter is allowableAccountTypes:

allowableAccountTypes

an optional string array of account types. These are used both to filter the shown accounts and to filter the list of account types that are shown when adding an account.

For IMAP accounts in Email app that type is being returned as "com.google.android.legacyimap" (please do not log accounts' details in production):

AccountManager accountManager = AccountManager.get(getApplicationContext());
Account[] accounts = accountManager.getAccountsByType(null);
for (Account account : accounts) {
    Log.d(TAG, "account: " + account.name + " : " + account.type);
}

That's using (add all account types you need to the array)

Intent intent = AccountPicker.newChooseAccountIntent(null, null,
        new String[] {"com.google", "com.google.android.legacyimap"},
        false, null, null, null, null);

is returning following on my device:

Choose an account

Please note that AccountPicker class is part of Google Play Services package, one could use AccountManager.newChooseAccountIntent() (added in API level 14) instead and get the same results.

Hope this helps.

Stockmon answered 30/10, 2014 at 23:54 Comment(4)
What gradle dependency you used for this? Including compile 'com.google.android.gms:play-services:9.0.2' can lead to 65k limit.Savino
@ShubhamA. Sorry, it's been a while already and I don't remember. If your min SDK version is 14+ you may consider using AccountManager.newChooseAccountIntent() instead.Stockmon
@Stockmon This call an intent which show up a list of accounts from the device . but if no account exists then it redirects me to google Sign In Windows (Which comes at first boot account login) . I didn't see any parameter in newChoolseAccoutnIntent . How to get off this situation ?Evangelista
@xaif: please post a new question and define "How to get off this situation?"Stockmon
T
12

After Digging around, I finally ended up downloading every related app (outlook, linkedin, twitter..) and dumping there account types using the following code:

public void pickUserAccount() {
    /*This will list all available accounts on device without any filtering*/
    Intent intent = AccountPicker.newChooseAccountIntent(null, null,
            null, false, null, null, null, null);   
    startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT);
}
/*After manually selecting every app related account, I got its Account type using the code below*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
        // Receiving a result from the AccountPicker
        if (resultCode == RESULT_OK) {
            System.out.println(data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE));
            System.out.println(data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, R.string.pick_account, Toast.LENGTH_LONG).show();
        }
    }
}

And this is the following results that I got:

  • Outlook (Hotmail, Live):com.outlook.Z7.eas
  • LinkedIn: com.linkedin.android
  • Facebook: com.facebook.auth.login
  • Twitter: com.twitter.android.auth.login
  • Every other Imap email accounts used in Android Mail app: com.google.android.legacyimap (Thanks to Ozbek)
  • and of course Google: com.google

I am still missing the yahoo account type, cause the yahoo mail app kept crashing on my device.

Therefore I hope that if you have yahoo's account type, please share it.

REVISION 7-12-2015 with a Better Solution

Pattern emailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(getActivity()).getAccounts();
ArrayList<String> emails = new ArrayList<String>();
for (Account account : accounts) {
    if (emailPattern.matcher(account.name).matches()) {
         emails.add(account.name);
    }
}
Twoseater answered 6/1, 2015 at 18:26 Comment(1)
com.microsoft.office.outlook.USER_ACCOUNT seems to be the current type for Outlook, Live, etc., in 2023.Padron
M
4

It's 2019, and the code does not seem to work anymore. To get all accounts shown in the picker (using Xamarin Android), instead of

Android.Gms.Common.AccountPicker.NewChooseAccountIntent(null, null, 
null, false, null, null, null, null);

you have to use

Android.Accounts.AccountManager.NewChooseAccountIntent(null,null,null,null,null,null,null)
Munafo answered 13/12, 2019 at 9:58 Comment(2)
The question is specifically about AccountPicker.newChooseAccountIntent() class/method.Stockmon
You may be right... (why was it selected as an answer?)Munafo

© 2022 - 2024 — McMap. All rights reserved.