我得到了这个问题的解决方案。
问题:删除/添加帐户仍为空account
object
解决方案一:
首先我使用删除帐户removeAccount()
然后尝试addAccountExplicitly
BUT removeAccount()
花时间执行后台线程 while 显式添加帐户打电话并做进一步的处理。
所以我改变了我的流程,就像我以前使用的那样removeAccount
的方法客户经理类并在该处理程序中执行整个过程,因此我在回调区域内编写代码。
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
mAccountManager.removeAccount(accounts[0], LoginActivity.this, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
// Creating the account on the device and setting the auth token we got
// (Not setting the auth token will cause another call to the server to authenticate the user)
mAccountManager.addAccountExplicitly(account, accountPassword, intent.getBundleExtra(AccountManager.KEY_USERDATA));
mAccountManager.setAuthToken(account, authTokenType, authToken);
/**
* Setting for Sync Adapter
* Syncing Configuration
*/
SyncAdapter.configSyncAdapter(mContext);
}
}, null);
} else {
mAccountManager.removeAccount(accounts[0], new AccountManagerCallback<Boolean>() {
@Override
public void run(AccountManagerFuture<Boolean> future) {
// Creating the account on the device and setting the auth token we got
// (Not setting the auth token will cause another call to the server to authenticate the user)
mAccountManager.addAccountExplicitly(account, accountPassword, intent.getBundleExtra(AccountManager.KEY_USERDATA));
mAccountManager.setAuthToken(account, authTokenType, authToken);
/**
* Setting for Sync Adapter
* Syncing Configuration
*/
SyncAdapter.configSyncAdapter(mContext);
}
}, null);
}
解决方案2:
我发现方法称为重命名帐户() https://developer.android.com/reference/android/accounts/AccountManager.html#renameAccount(android.accounts.Account,%20java.lang.String,%20android.accounts.AccountManagerCallback%3Candroid.accounts.Account%3E,%20android.os.Handler)但它至少需要 sdk 版本 21。
根据文档:
重命名指定的帐户。这相当于删除
现有帐户并使用旧帐户添加新的重命名帐户
帐户的用户数据。
从主线程调用此方法是安全的。
谢谢。