authToken重置后android getAuthToken挂起

时间:2015-10-12 04:33:02

标签: android accountmanager

我在android AccountManager中提供的getAuthToken()有问题(步骤如下):

  1. 将authToken重置为null(在注销过程中)
  2. 使新的null authToken
  3. 无效
  4. 将新的authToken设置为服务器提供的新字符串(登录返回)
  5. 使新字符串authToken
  6. 无效
  7. 并尝试检查/获取新的authToken,
  8. 但是在通过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);
    }
    

1 个答案:

答案 0 :(得分:1)

尽量避免使用像这样的活动

future = accountManager.getAuthToken(account, authType, null, true, null, null);