Android AccountManager.getUserData()返回null

我有一个类似的问题,像这个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

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

不要使用addAccountExplicitly(Account, String, Bundle)添加用户数据。 相反,只需传递一个空包并使用setUserData(Account, String, String)在使用setUserData(Account, String, String)创建帐户添加用户数据。

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

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

  • 只有通过“设置”中的帐户手动删除帐户后创建的帐户才会出现此问题。
  • 重新启动设备或重新安装应用程序可以解决问题(直到第一个点再次发生)。
  • 到目前为止,我只看到了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。 然后,您可以使用此方法计算帐户是否被手动删除。 如果不是,则不需要每次都调用此方法。