Android AccountManager.getAuthToken()在Samsung和Moto G设备上发布版本时挂起

时间:2016-04-05 16:06:24

标签: android authentication accountmanager accounts

我正在使用AccountManager来检索帐户的身份验证令牌,但帐户经理会挂起并且永远不会返回结果。我原本试图直接从未来获得它但这总是引起了悬念。我切换到使用AccountManagerCallback但回调永远不会执行。有点奇怪的是a)我没有在任何调试版本中遇到这个问题。仅在发布版本和b)仅在三星和Moto G手机上。

这是我的回调实现

private class OnAccountManagerComplete implements AccountManagerCallback<Bundle> {

    private Account account;

    public OnAccountManagerComplete(Account account) {
        this.account = account;
    }

    @Override
    public void run(AccountManagerFuture<Bundle> future) {
        Bundle bundle;
        try {
            bundle = future.getResult();
            final String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
            getDelegate().getConfiguration().setAccessToken(authToken);
            ((PregnancyActivityDelegate) getDelegate()).getRestService().activateProduct(account.name,
                    mActivateCallback);
        } catch (AuthenticatorException | OperationCanceledException | IOException e) {
            Log.e(OnAccountManagerComplete.class.getName(), e.getLocalizedMessage());
            e.printStackTrace();
            mProgressDialog.dismiss();
            Intent intent = new Intent(StartActivity.this, LoginActivity.class);
            intent.putExtra(LoginActivity.EXTRA_LOGIN_TYPE, LoginActivity.SIGN_IN);
            startActivity(intent);
        }
    }
}

这是我调用getAuthToken

的地方
private void getExistingAuthToken(final Account account, String authTokenType) {
    mProgressDialog = ProgressDialog.show(StartActivity.this, "Retrieving Auth Token", "", true);
    Bundle options = new Bundle();
    mAccountManager.getAuthToken(account, authTokenType, options, this, new OnAccountManagerComplete(account), null);
}

为了彻底,这是我的身份验证员课程。它是一个抽象类,因为它驻留在由我们的几个应用程序实现的共享库中

// region Public Static Constants
public static final String KEY_ACCOUNT_TYPE = "Ovia:AccountType";
public static final String KEY_AUTH_TYPE = "Ovia:AuthType";
public static final String KEY_NEW_ACCOUNT = "Ovia:IsNewAccount";

public static final String ACCOUNT_TYPE = "com.ovuline.ovia";
// endregion Public Static Constants

// region Private Members
private Context context;
// endregion Private Members

// region Constructors
public OviaAccountAuthenticator(Context context) {
    super(context);
    this.context = context;
}
// endregion Cosntructors

// region Parent Class Overrides
@Override
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
    return null;
}

@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
    final Intent authActivityIntent = getLoginIntent(context);
    authActivityIntent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
    authActivityIntent.putExtra(AccountManager.KEY_ACCOUNT_NAME, authTokenType);
    authActivityIntent.putExtra(KEY_NEW_ACCOUNT, true);
    authActivityIntent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, authActivityIntent);
    return bundle;
}

@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
    return null;
}

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    final AccountManager accountManager = AccountManager.get(context);

    // Retrieve auth token from the account manager
    String authToken = accountManager.peekAuthToken(account, authTokenType);

    // If we found an auth token, return it in the bundle.  Else, launch the login activity
    // so a user can authenticate.
    if (!TextUtils.isEmpty(authToken)) {
        final Bundle authBundle = new Bundle();
        authBundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        authBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        authBundle.putString(AccountManager.KEY_AUTHTOKEN, authToken);
        return authBundle;
    }

    final Intent authIntent = getLoginIntent(context);
    authIntent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    authIntent.putExtra(KEY_ACCOUNT_TYPE, account.type);
    authIntent.putExtra(KEY_AUTH_TYPE, authTokenType);
    final Bundle authBundle = new Bundle();
    authBundle.putParcelable(AccountManager.KEY_INTENT, authIntent);
    return authBundle;
}

@Override
public String getAuthTokenLabel(String authTokenType) {
    return authTokenType + " (label)";
}

@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    return null;
}

@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
    final Bundle featuresBundle = new Bundle();
    featuresBundle.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
    return featuresBundle;
}
// endregion Parent Class Overrides

protected abstract Intent getLoginIntent(Context context);

您可以就此问题向我提供的任何见解都非常有用。

0 个答案:

没有答案