How to remove account in AccountManager in Android
Solution 1
Try this it will work
// Global Variables
public static final String AUTHORITY = "com.example.package";
public static final String ACCOUNT_TYPE = "com.example.package";
public static final String ACCOUNT = "my_custom_account_name";
// Account Manager definition
AccountManager accountManager = (AccountManager) this.getSystemService(ACCOUNT_SERVICE);
// loop through all accounts to remove them
Account[] accounts = accountManager.getAccounts();
for (int index = 0; index < accounts.length; index++) {
if (accounts[index].type.intern() == AUTHORITY)
accountManager.removeAccount(accounts[index], null, null);
}
requires
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
Solution 2
You need to override the following method in the Authenticator class from AbstractAccountAuthenticator.
public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response, Account account) {
Bundle result = new Bundle();
boolean allowed = true; // or whatever logic you want here
result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, allowed);
return result;
}
Solution 3
Had the same problem
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP_MR1) {
accountManager.removeAccount(account, {}, AContext.app.mainHandler)
} else {
accountManager.removeAccountExplicitly(account)
}
For API 22 and higher works perfectly, but on API 19 didn't work at all.
Finally found the problem in my implementation of AbstractAccountAuthenticator:
override fun getAccountRemovalAllowed(response: AccountAuthenticatorResponse?, account: Account?): Bundle {
AccountHelper.removeAccount()
return super.getAccountRemovalAllowed(response, account)
}
It became to work after deleting "AccountHelper.removeAccount()"
I don't know - maybe it'll help
Solution 4
You have to call 2 below methods before removeAccount method and the system will allow you to remove the account in account manager. clearPassword invalidateAuthToken
Based on the description on the removeAccount method:
"The authenticator may have its own policies preventing account deletion, in which case the account will not be deleted."
Have fun.
Vico
Updated on March 05, 2020Comments
-
Vico about 4 years
I am trying to remove a custom account in AccountManager.
This is my code :
final Handler handler = new Handler (); AccountManagerCallback<Boolean> callback = new AccountManagerCallback<Boolean>() { @Override public void run(AccountManagerFuture<Boolean> arg0) { String test = "test"; } }; AccountManagerFuture<Boolean> bool = am.removeAccount(account, callback, handler);
Permissions I'm using :
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.MANAGE_ACCOUNTS"></uses-permission> <uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission> <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission> <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"></uses-permission>
The account is never removed and the callback never called, any idea ? No trace in logs
-
Vico about 11 yearsThanks but it doesn't work. You're basically doing the same thing as me. The removeAccount() function doesn't do anything (and return nothing btw).
-
DjHacktorReborn about 11 years@user2083698 you might be doing something wrong its works perfect in my app
-
DjHacktorReborn about 11 years@user2083698 make sure you have above permission and your authority matches in if
-
Vico about 11 yearsAdding this permission does not change anything. I posted all enabled permission on the first post. And removing account from android's settings doesn't work neither.
-
Vico about 11 yearsIs it possible for you to show me a sample project which creates and removes an account ?
-
DjHacktorReborn about 11 yearstry Account[] accounts = accountManager.getAccounts(); for (int index = 0; index < accounts.length; index++) {}
-
Vico about 11 yearsAccount is actually found but when I call the removeAccount() function on this account nothing happens.
-
Shirish Herwade almost 10 yearsYou are wrong, doesn't need WRITE_SYNC_SETTINGS for above code, instead of need 'GET_ACCOUNTS' and 'MANAGE_ACCOUNTS' permission. Plz correct me if I'm wrong.
-
AZ_ over 9 years@DjHacktorReborn TableConstants.AUTHORITY ?? what is that String
-
DjHacktorReborn over 9 years@AZ_ its the unique identifier needed for content uri
-
AZ_ over 9 years@DjHacktorReborn Your code is working man. Only we have to allow removal in Authenticator class. +1
-
Nitish over 9 years@DjHacktorReborn For adding or removing accounts, our app needs to hold
android.permission.MANAGE_ACCOUNTS
. See [(developer.android.com/reference/android/accounts/…, android.accounts.AccountManagerCallback<java.lang.Boolean>, android.os.Handler))] -
Brandon about 9 years@DjHacktorReborn -- I updated your code to make it clearer. But thank you for posting it, it really helped me out (just needed some clarification which I added in the edit).
-
a.t. over 5 yearsCould you elaborate on how to override the method in the
Authenticator
class fromAbstractAccountAuthenticator
? From what I understand from your answer, assuming Vico's code is in some method namedpublic void removing(){}
, one would first have to call the classAbstractAccountAuthenticator
from somewhere, and then in that class in Vico's methodremoving
one should paste your methodgetAccountRemovalAllowed
with the text@Override
above it. But I am having a bit diffuculty understanding how you could reach classAbstractAccountAuthenticator
from Vicos method
removing`.