我试图按照验证身份OAuth2服务并实施,其中一个目的是包含在由AccountManagerFuture#的getResult()调用。
提供的捆绑的一部分问题是,即使文档说使用活动#startActivityForResult(...),目的有人告诉我火显然开始在自己的任务,导致onActivityResult立即被调用。
这我不清楚,我做正确的另一部分是我推出这个意图的方式。因为code调用的AccountManager#getAuthToken(...)被埋工人线程与为当前活动进不去里面,我推出一个新的活动我称之为CredentialsActivity,然后使用启动操作系统提供的意图startActivityForResult。
这是我如何做到这一点:
最后AccountManagerFuture<包>未来= AccountManager.getAuthToken(...);
//现在我们有了未来,我们提取了捆绑
捆绑包= NULL;
尝试 {
捆绑= future.getResult();
}赶上(例外五){
log.warn(即得到了一个异常);
}
如果(丛== NULL){
log.info(无法获取身份验证令牌);
返回;
}
//检查用户需要输入凭据。
最终意图askForPassword =(意向)bundle.get(AccountManager.KEY_INTENT);
如果(askForPassword!= NULL){
log.dev(需要提示输入凭据,射击意图......);
CredentialsActivity.promptForCredentials(背景下,askForPassword);
}
这些都是CredentialsActivity的相关部分:
私有静态最终诠释REQUEST_ code_LAUNCH_CREDENTIALS_INTENT = 0;
私有静态意图newCredentialsActivityIntent(上下文的背景下){
最终意向意图=新的意图(背景下,CredentialsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
返回的意图;
}
公共静态无效promptForCredentials(上下文的背景下,意图credentialsIntent){
最终意向意图= newCredentialsActivityIntent(上下文);
intent.putExtra(Intent.EXTRA_INTENT,credentialsIntent);
context.startActivity(意向);
}
我火在onResume的意图:
@覆盖
保护无效onResume(){
super.onResume();
最终意向意图= getIntent();
最终意图credentialsIntent =(意向)intent.getParcelableExtra(Intent.EXTRA_INTENT);
如果(credentialsIntent!= NULL){
startActivityForResult(credentialsIntent,REQUEST_ code_LAUNCH_CREDENTIALS_INTENT);
}
}
解决方案
好了,我想我这一个 - 我会后的答案,以防万一:
的问题是,该系统提供与NEW_TASK标志集的意图。我需要将其清除,使这项工作对我来说:
最终意图credentialsIntent =(意向)intent.getParcelableExtra(Intent.EXTRA_INTENT);
如果(credentialsIntent!= NULL){
credentialsIntent.setFlags(0);
startActivityForResult(credentialsIntent,REQUEST_ code_LAUNCH_CREDENTIALS_INTENT);
}
I am trying to follow Authenticating to OAuth2 Services and implement the part where an Intent is included in the Bundle provided by the AccountManagerFuture#getResult() call.
The issue is, that even though the docs say to use Activity#startActivityForResult(...), the Intent I am told to fire apparently starts in its own task, resulting in onActivityResult being called immediately.
Another part which I am uncertain I am doing correctly is the way I launch this Intent. Because the code that calls AccountManager#getAuthToken(...) is buried inside a worker thread with no access to the current Activity, I am launching a new Activity I call "CredentialsActivity" which then launches the OS-provided Intent using startActivityForResult.
This is how I do that:
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);
}
These are the relevant parts of 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);
}
I fire the Intent in 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);
}
}
解决方案
OK, so I think I figured this one out - I'll post the answer, just in case:
The issue is that the system provides the Intent with the NEW_TASK flag set. I needed to clear it to make this work for me:
final Intent credentialsIntent = (Intent) intent.getParcelableExtra(Intent.EXTRA_INTENT);
if (credentialsIntent != null) {
credentialsIntent.setFlags(0);
startActivityForResult(credentialsIntent, REQUEST_CODE_LAUNCH_CREDENTIALS_INTENT);
}
相关推荐
最新文章