Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

android - Pick an email using AccountPicker.newChooseAccountIntent

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...