How to cancel a periodic sync when an app gets his data reset? And how do I remove the related account when that app is uninstalled?

StackOverflow https://stackoverflow.com/questions/22214551

Question

I just realized that the periodic sync continues to be triggered when my application data is reset from the setting, and I do not want that to happen.

Looking to solve this, I noticed the AccountManager.removeAccount() method but I am not sure about how this would affect the syncs my SyncAdapter periodically does, and its documentation does not mention anything in that regard. And I don't know where to call it.

If there is no way to cancel the scheduled periodic sync operation when my app data are deleted, I could introduce checks in the sync code for some obvious clue that this happened. But will it suffice to call AccountManager.removeAccount()? Or will this raise some kind of exception at runtime (e.g. if the code to perform the sync will be executed, I imagine the account will be null and so a NullPointerException may be generated)

Was it helpful?

Solution

You can subscribe to this broadcast to detect clearing data from settings. Note however that this will be handled in another thread than your SyncAdapter is working, so you'll have to introduce some checks to your sync adapters code anyways.

Your other issue - your apps account and sync adapter being kept after app is uninstalled - is really weird. Can you post your AndroidManifest.xml and authenticator service metadata?

OTHER TIPS

Usually the account get removed when the application is uninstalled. To use Accountmanager's removeaccount method, below is the sample code.

    AccountManager mgr = AccountManager.get(getApplicationContext());
    mgr.invalidateAuthToken(AUTHORITY , null);
    Account[] accounts = mgr.getAccounts();

    for (int index = 0; index < accounts.length; index++) {
        if (accounts[index].type.intern() == AUTHORITY)
            mgr.removeAccount(accounts[index], null, null);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top