我在android AccountManager中提供的getAuthToken()有问题(步骤如下):
但是在通过getAuthToken()方法获取新的authToken时,调用 future.getResult()永远挂起。在帐户创建期间首次登录*时不会发生这种情况。我能够使用相同的可调用类来获取身份验证令牌。
以下是我定义的可调用类。请告知如何解决这个问题。
private AccountManagerFuture<Bundle> future = null;
private String authToken;
class GetAuthTokenTask implements Callable<Bundle> {
private AccountManager accountManager;
private Account account;
private String authType;
private Activity activity;
public GetAuthTokenTask(AccountManager accountManager, Account account, String authType, Activity activity) {
this.accountManager = accountManager;
this.account = account;
this.authType = authType;
this.activity = activity;
}
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
@Override
public Bundle call() throws Exception {
return getAuthToken();
}
private Bundle getAuthToken() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
future = accountManager.getAuthToken(account, authType, null, activity, null, null);
try {
Bundle result = future.getResult();
if (result!=null) {
authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
}
} catch (OperationCanceledException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (AuthenticatorException e) {
e.printStackTrace();
}
Bundle output = new Bundle();
output.putString(AccountManager.KEY_AUTHTOKEN, authToken );
return output;
}
}
//caller method
private String getAuthToken(Account account, String authType) {
ExecutorService es = Executors.newSingleThreadExecutor();
GetAuthTokenTask authTokenTask = new GetAuthTokenTask(accountManager, account, authType, (Activity)getBaseContext());
FutureTask<Bundle> result = new FutureTask<Bundle>(authTokenTask);
es.execute(result);
Bundle resultBundle = new Bundle();
try {
resultBundle = result.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return resultBundle.getString(AccountManager.KEY_AUTHTOKEN);
}
答案 0 :(得分:1)
尽量避免使用像这样的活动
future = accountManager.getAuthToken(account, authType, null, true, null, null);