Not able to remove my own custom-account

烂漫一生 提交于 2019-12-10 15:58:09

问题


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:

  1. Always get account objects from the AccountManager to change them.

    final AccountManager accountManager = AccountManager.get(this);
    accountManager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
    
  2. Make sure if you're overriding getAccountRemovalAllowedon your Authenticator, 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!