Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove account in AccountManager in Android

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

like image 752
Vico Avatar asked Mar 11 '13 09:03

Vico


People also ask

What is AccountManager in Android?

android.accounts.AccountManager. This class provides access to a centralized registry of the user's online accounts. The user enters credentials (username and password) once per account, granting applications access to online resources with "one-click" approval.

What is Account Manager app?

Accounts Manager app can be used to track your daily income and expense transaction as per your need. Easy Entries: Account Manager App is easy in adding, deleting and canceling a credit or debit entry.

Is Android account manager secure?

Using an AccountManager to store credentials is a much secure way than storing in a file or a SQL DB. A file can be retrieved by any other app unlike via AccountManager Android will enforce that only your app will be able to access to the key.


2 Answers

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

like image 93
Andrey Turkovsky Avatar answered Nov 09 '22 22:11

Andrey Turkovsky


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" />
like image 41
DjHacktorReborn Avatar answered Nov 09 '22 22:11

DjHacktorReborn