我想让用户在这里选择他们的Google帐户。在我使用“setSelectedAccoutName”方法后,我尝试打印出所选的帐户名称,该名称返回null。我100%确定accountName不为null。那么有谁知道为什么mCredential.getSelectedAccountName为null?
private void pickContact() {
AccountManager accountManager = AccountManager.get(getActivity().getApplicationContext());
Account[] accounts = accountManager.getAccountsByType(null);
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
new String[]{"com.google"},
false, null, null, null, null);
startActivityForResult(intent, 23);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("hi");
if (requestCode == 23) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
String accountName = bundle.getString(AccountManager.KEY_ACCOUNT_NAME);
System.out.println(accountName);
if (accountName != null) {
mCredential.setSelectedAccountName(accountName);
System.out.println(mCredential.getSelectedAccountName());
getResultsFromApi();
} else {
pickContact();
}
}
}
}