解雇从AccountManagerFuture收到的Intent时捕获活动结果的问题

时间:2012-11-21 17:17:15

标签: android android-intent android-activity oauth accountmanager

我正在尝试关注Authenticating to OAuth2 Services并实现AccountManagerFuture#getResult()调用提供的Bundle中包含Intent的部分。

问题是,即使文档说使用Activity#startActivityForResult(...),我被告知要触发的Intent显然是在自己的任务中启动,导致onActivityResult被立即调用。

我不确定我正确做的另一部分是我启动此Intent的方式。因为调用AccountManager#getAuthToken(...)的代码被隐藏在工作线程中而无法访问当前的Activity,所以我启动了一个新的Activity,我调用了#34; CredentialsActivity"然后使用startActivityForResult启动操作系统提供的Intent。

我就是这样做的:

      final AccountManagerFuture<Bundle> future = AccountManager.getAuthToken(...);

      // Now that we have the Future, we extract the Bundle
      Bundle bundle = null;
      try {
        bundle = future.getResult();
      } catch (Exception e) {
        log.warn(e, "Got an Exception");
      }

      if (bundle == null) {
        log.info("Unable to get auth token");
        return;
      }

      // Check if the user needs to enter credentials.
      final Intent askForPassword = (Intent) bundle.get(AccountManager.KEY_INTENT);
      if (askForPassword != null) {
        log.dev("Need to prompt for credentials, firing Intent...");
        CredentialsActivity.promptForCredentials(context, askForPassword);
      }

这些是CredentialsActivity的相关部分:

      private static final int REQUEST_CODE_LAUNCH_CREDENTIALS_INTENT = 0;

      private static Intent newCredentialsActivityIntent(Context context) {
        final Intent intent = new Intent(context, CredentialsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
      }

      public static void promptForCredentials(Context context, Intent credentialsIntent) {
        final Intent intent = newCredentialsActivityIntent(context);
        intent.putExtra(Intent.EXTRA_INTENT, credentialsIntent);

        context.startActivity(intent);
      }

我在onResume中激活Intent:

  @Override
  protected void onResume() {
    super.onResume();
    final Intent intent = getIntent();
    final Intent credentialsIntent = (Intent) intent.getParcelableExtra(Intent.EXTRA_INTENT);
    if (credentialsIntent != null) {
      startActivityForResult(credentialsIntent, REQUEST_CODE_LAUNCH_CREDENTIALS_INTENT);
    }
  }

1 个答案:

答案 0 :(得分:0)

好的,所以我想我想出了这个 - 我会发布答案,以防万一:

问题是系统为Intent提供了NEW_TASK标志集。我需要清除它才能使我的工作:

final Intent credentialsIntent = (Intent) intent.getParcelableExtra(Intent.EXTRA_INTENT);
if (credentialsIntent != null) {
  credentialsIntent.setFlags(0);
  startActivityForResult(credentialsIntent, REQUEST_CODE_LAUNCH_CREDENTIALS_INTENT);
}