How to remove account in AccountManager in Android

Vico picture Vico · Mar 11, 2013 · Viewed 19.4k times · Source

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

Answer

DjHacktorReborn picture DjHacktorReborn · Mar 11, 2013

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" />