也许它会对某人有所帮助,在 2020 年根据文档使用它:
Intent intent =
AccountPicker.newChooseAccountIntent(
new AccountChooserOptions.Builder()
.setAllowableAccountsTypes(Arrays.asList("com.google"))
.build());
startActivityForResult(intent, SOME_REQUEST_CODE);
您也可以使用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);
您可以从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);
}