3

尽管已设置,但我有类似的问题,例如AccountManager getUserData 返回 null但解决方案对我不起作用

我的 Authenticator.java

public class Authenticator extends AbstractAccountAuthenticator{

    private Context context;
    public Authenticator(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response,
            String accountType) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response,
            String accountType, String authTokenType,
            String[] requiredFeatures, Bundle options)
            throws NetworkErrorException {
        Bundle result = new Bundle();
        Intent intent = new Intent(context,LoginActivity.class);
        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
        result.putParcelable(AccountManager.KEY_INTENT, intent);
        return result;
    }

    @Override
    public Bundle confirmCredentials(AccountAuthenticatorResponse response,
            Account account, Bundle options) throws NetworkErrorException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse response,
            Account account, String authTokenType, Bundle options)
            throws NetworkErrorException {

        return null;
    }

    @Override
    public String getAuthTokenLabel(String authTokenType) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Bundle updateCredentials(AccountAuthenticatorResponse response,
            Account account, String authTokenType, Bundle options)
            throws NetworkErrorException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Bundle hasFeatures(AccountAuthenticatorResponse response,
            Account account, String[] features) throws NetworkErrorException {

        return null;
    }

}

我添加了一个这样的帐户

mAccount = new Account("SalesGenie", "com.ambertag.salesgenie.ACCOUNT"); 
mAccountManager.addAccountExplicitly(mAccount, password, userData);

但是当我尝试通过调用来获取用户数据时,getUserData()我得到了 null

4

3 回答 3

11

有一个更简单的解决方案:

不要使用addAccountExplicitly(Account, String, Bundle). 相反,只需传递一个空包并setUserData(Account, String, String)使用.addAccountExplicitly

它可能不太方便,但它会确保用户数据缓存被正确填充并且不会返回null

于 2015-04-21T15:11:15.337 回答
1

经过一些测试,我发现了这些问题。

  • 该问题仅出现在通过“设置”中的帐户手动删除帐户后创建的帐户上。
  • 重新启动设备或重新安装应用程序可以解决问题(直到第一个点再次发生)。
  • 到目前为止,我只在 HTC 上看到过这个问题

解决方案:

在 Authenticator 的 addAccount() 中调用以下方法。

/**
 * This method is a hack to fix a bug that is found on HTC devices.
 *
 * The issue is basically when ever an account is manually deleted through the devices
 * settings the user data that is saved in the next account that is created is not accessible.
 * The solution is to create a temp account and delete it from the app instead. Deleting
 * accounts via the AccountManager gets around the bug.
 */
private void htcAccountSyncHack() {
    Context appContext = [Application].getInstance();
    Account account = new Account([accountName], [accountType]);
    AccountManager accountManager = (AccountManager) appContext.getSystemService(
            Context.ACCOUNT_SERVICE);
    accountManager.addAccountExplicitly(account, null, null);
    accountManager.removeAccount(account, null, null);
    SharedprefsMgr.setBooleanOnSessionSets(SharedprefsMgr.ACCOUNT_MANUALLY_DELETED, false);
}

理想情况下,您将拥有一个在 AccountManager 中注册为 OnAccountsUpdateListener 的 ContentProvider。然后,您可以使用它来确定该帐户是否被手动删除。如果不是,则无需每次都调用此方法。

于 2015-03-31T00:31:57.093 回答
0

Marten 的回答并没有在我们的应用程序中解决这个问题。文档说“从主线程调用这个方法是安全的”,我们的应用程序正在后台线程上调用,AccountManager.getUserData()这通常工作正常,但显然不能保证工作。

于 2019-12-15T00:09:21.573 回答