Java Android AccountManager.getUserData()返回null

Java Android AccountManager.getUserData()返回null,java,android,android-account,Java,Android,Android Account,我有类似的问题,但解决方案对我不起作用 My Authenticator.java public class Authenticator extends AbstractAccountAuthenticator{ private Context context; public Authenticator(Context context) { super(context); this.context = context; } @O

我有类似的问题,但解决方案对我不起作用

My 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()
获取用户数据时,经过一些测试,我发现了这些问题

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

在验证器的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。然后,您可以使用它来计算该帐户是否被手动删除。如果不是,则无需每次调用此方法。

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

不要使用添加用户数据。相反,只需传递一个空包,并在使用
addaccountexplicit
创建帐户后使用添加用户数据


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

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

感谢您的回复。。据我所知,我已经解决了所有这些问题,+1表示有关setUserData(帐户、字符串、字符串)的信息