使用AccountPicker.newChooseAccountIntent选择一封电子邮件

Udi*_*shi 19 android android-account accountpicker

我试图让用户使用以下代码选择一个电子邮件帐户:

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

此代码效果很好,但如果用户没有Gmail帐户,但雅虎,Hotmail等.如何通过更改第三个参数显示所有电子邮件帐户:

new String[]{"com.google"}
Run Code Online (Sandbox Code Playgroud)

非常感谢你

ozb*_*bek 23

根据文档,第三个参数是allowableAccountTypes:

allowableAccountTypes

帐户类型的可选字符串数组.这些用于过滤显示的帐户并过滤添加帐户时显示的帐户类型列表.

对于电子邮件应用中的IMAP帐户,该类型正在返回"com.google.android.legacyimap" (请不要在生产中记录帐户的详细信息):

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

正在使用(将所需的所有帐户类型添加到数组中)

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

正在我的设备上返回以下内容:

选择一个帐户

请注意,该AccountPicker课程是Google Play服务套餐的一部分,可以使用AccountManager.newChooseAccountIntent()(在API级别14中添加),并获得相同的结果.

希望这可以帮助.


Bas*_*jan 8

在挖掘之后,我终于下载了每个相关应用程序(outlook,linkedin,twitter ..)并使用以下代码转储帐户类型:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的以下结果:

  • Outlook(Hotmail,Live):com.outlook.Z7.eas
  • LinkedIn: com.linkedin.android
  • Facebook的: com.facebook.auth.login
  • 推特: com.twitter.android.auth.login
  • Android Mail应用程序中使用的每个其他Imap电子邮件帐户:( com.google.android.legacyimap感谢Ozbek)
  • 当然谷歌: com.google

我仍然缺少雅虎帐户类型,导致雅虎邮件应用程序一直在我的设备上崩溃.

因此,我希望如果你有雅虎的帐户类型,请分享.

修订7-12-2015,提供更好的解决方案

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Rye*_*ead 4

现在已经是 2019 年了,代码似乎不再起作用了。要获取选择器中显示的所有帐户(使用 Xamarin Android),而不是

Android.Gms.Common.AccountPicker.NewChooseAccountIntent(null, null, 
null, false, null, null, null, null);
Run Code Online (Sandbox Code Playgroud)

你必须使用

Android.Accounts.AccountManager.NewChooseAccountIntent(null,null,null,null,null,null,null)
Run Code Online (Sandbox Code Playgroud)

  • 问题具体是关于“AccountPicker.newChooseAccountIntent()”类/方法。 (2认同)