问题
I have googled for this problem, but I did not find any solution.
I have created my own Custom-Account. When I'm trying to remove the account programmatically using the following code, the account is not be removed:
Account systemAccount = new Account(mainAccount.getDisplayName(),
getResources().getString(R.string.account_type));
AccountManager.get(Accounts.this).removeAccount(systemAccount, null, null);
Even, when I try to remove the account from Setting, nothing happened. the account is removed only when I uninstall the application.
What should I do?
回答1:
You are not using the Future
passed as a parameter to the AccountManagerCallback<Boolean>#run
method.
You should provide the callback as the second parameter to: public AccountManagerFuture<Boolean> removeAccount (Account account, AccountManagerCallback<Boolean> callback, Handler handler)
myAccountManager.removeAccount(myAccount, new AccountManagerCallback<Boolean>() {
@Override
public void run(AccountManagerFuture<Boolean> future) {
// This is the line that actually starts the call to remove the account.
boolean wasAccountDeleted = future.getResult();
}
}, null);
You should be careful how you call future.getResult()
. It should not be called on the main UI thread. This example does not provide that mechanism for brevity.
回答2:
Two things:
Always get account objects from the
AccountManager
to change them.final AccountManager accountManager = AccountManager.get(this); accountManager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
Make sure if you're overriding
getAccountRemovalAllowed
on yourAuthenticator
, you're returning the Bundle with a boolean value of true, this is the default behaviour.public Bundle getAccountRemovalAllowed( AccountAuthenticatorResponse response, Account account) throws NetworkErrorException { final Bundle result = new Bundle(); result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); return result; }
来源:https://stackoverflow.com/questions/15358271/not-able-to-remove-my-own-custom-account