从客户经理中删除帐户

remove account from Account Manager

我尝试使用此代码删除帐户,但它不起作用:

{
    AccountManager accMgr = AccountManager.get(getApplicationContext());
    Account[] accounts = accMgr.getAccountsByType(AccountGeneral.ACCOUNT_TYPE);
    for (Account ac : accounts) {
        accMgr.removeAccount(ac, null, null);
    }
}

即使我的身份验证器中也有此代码:

@Override
    public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response, Account account) throws NetworkErrorException {
        Bundle result = new Bundle();
        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
        return result;
    }

有什么问题吗?

答案是这一行

accMgr.getAccountsByType(AccountGeneral.ACCOUNT_TYPE);

在这里,我按错误的类型获取帐户,我必须获取所有帐户并循环删除它,所以答案是将这一行替换为 getAccounts()

{

                    AccountManager accMgr = AccountManager.get(getApplicationContext());
                    Account[] accounts = accMgr.getAccounts();
                    for (Account ac : accounts) {
                        accMgr.removeAccount(ac, null, null);
                    }
            }