我正在使用 Intent newChooseAccountIntent
让用户选择一个帐户。我在这里读到, Intent 返回一个 bundle ,其中包含用于帐户名称的键 KEY_ACCOUNT_NAME
和用于帐户类型的 KEY_ACCOUNT_TYPE
。
https://developer.android.com/reference/android/accounts/AccountManager.html
我的问题是如何访问 Intent 返回的 bundle ?我会在 Activity 结果函数中执行 data.getBundleExtra()
吗?
Activity 结果函数代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
Log.d("frag", "is return empty " + data.getBundleExtra("KEY_ACCOUNT_NAME"));
}
else if (resultCode == RESULT_CANCELED) {
Log.d("frag", "intent fired and something went wrong");
}
}
最佳答案
根据 Javadoc,AccountManager.KEY_ACCOUNT_NAME和 AccountManager.KEY_ACCOUNT_TYPE是字符串,这意味着您使用 getStringExtra()
:
if(resultCode == RESULT_OK) {
String name = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
String type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
Log.d("frag", "Got " + name + ", " + type);
}
关于java - 如何访问 AccountManager.newChooseAccountIntent 返回的 bundle ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57334905/