我想要跟隨Authenticating to OAuth2 Services並實現由AccountManagerFuture#getResult()調用提供的Bundle中包含Intent的部分。問題捕獲時發生的活動結果從AccountManagerFuture收到的意圖
問題是,即使文檔說使用Activity#startActivityForResult(...),我被告知明確啓動的Intent開始在它自己的任務中,導致立即調用onActivityResult。
另一部分,我不確定我正在做的是我發佈這種意圖的方式。因爲調用AccountManager#getAuthToken(...)的代碼被隱藏在工作線程中,並且無法訪問當前的Activity,所以我啓動了一個新的Activity,我稱之爲「CredentialsActivity」,然後使用startActivityForResult啓動OS提供的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的意圖:
@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);
}
}