java - 如何访问 AccountManager.newChooseAccountIntent 返回的 bundle ?

标签 java android android-intent

我正在使用 Intent newChooseAccountIntent让用户选择一个帐户。我在这里读到, Intent 返回一个 bundle ,其中包含用于帐户名称的键 KEY_ACCOUNT_NAME 和用于帐户类型的 KEY_ACCOUNT_TYPEhttps://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_NAMEAccountManager.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/

相关文章:

java - Java中的模式匹配问题

java - 为什么在 android android.text.TextUtils.getChars 中出现 StackOverflowError?

android - 如何取消订阅广播接收器中的 RXjava 调用

android - 如何阻止特定资源在 webview 中加载

android - Gallery OnActivityResult 在 TabActivity 中不起作用

java - 膨胀布局后更改 TextView 的文本

java - HashMap 对于用户定义的对象失败?

java - 如何在 JSF 中检索 DIV 宽度

android - 通过 Android 中的 Intent 传递复杂对象

android - 自定义操作 Intent 在与我从中单击的应用程序相同的上下文中打开我的应用程序