Search code examples
androidauthenticationaccountmanager

remove account in "old" sdk


I try to write my account own AccountAuthenticator for my app using Android's AccountManager class. I have a "Log out" button which should delete an existing account and open LoginActivity again.

For this I use remove method of AccountManager:

 accountManager.removeAccount(account,null,new AccountManagerCallback<Bundle>() {
     @Override
     public void run(AccountManagerFuture<Bundle> future) {
       Bundle bnd = null;
       try {
           bnd = future.getResult();
           Log.d(TAG, "removing account  " + bnd);

           } catch (Exception e) {
                 e.printStackTrace();
                 Log.i(TAG, e.getMessage());
                 }
       }
    },null);

But I get an error message which tells me that this method can function only for SDKs that are later than 22. My Miniman SDK version is set up as 14. The same I get for the Method removeAccountExplicitly(account).

Is there any possibility to remove an account in older SDKs?


Solution

  • One possibility would be to use the other removeAccount method in AccountManager, which is deprecated since api 22 and has a minimum api level 5.

    If you change:

    .removeAccount(account,null,new AccountManagerCallback<Bundle>() {
    

    to:

    .removeAccount(account,new AccountManagerCallback<Boolean>() {
    

    and adapt a bit of your code, it might work.

    While it is not adviced nor good practice to use deprecated methods (they are deprecated for some reason) it might be a quick-fix for what you want to achieve.

    Hope it helps you.