开发者

Clearing data on account removal

开发者 https://www.devze.com 2023-02-28 12:35 出处:网络
I want to clear the application\'s data when a user manually removes an account from the Accounts & sync section i开发者_Go百科n the settings app.

I want to clear the application's data when a user manually removes an account from the Accounts & sync section i开发者_Go百科n the settings app.

I have my own implementation of AbstractAccountAuthenticator but there is no method to hook in the remove account process. Any hints?


I've been pondering on the same problem and here's the "solution" I decided upon. It's not what I'd call the "correct" solution but it's the best I believe you can manage with the current API.

In my implementation of the AbstractAccountAuthenticator class, I've overriden the getAccountRemovalAllowed function as follows:

@Override
public Bundle getAccountRemovalAllowed(
        AccountAuthenticatorResponse response, Account account)
        throws NetworkErrorException {
    Bundle result = super.getAccountRemovalAllowed(response, account);

    if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)
            && !result.containsKey(AccountManager.KEY_INTENT)) {
        final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT);

        if (removalAllowed) {
            // Do my removal stuff here
        }
    }

    return result;
}

There is a tiny chance that removal could fail AFTER you return from getAccountRemovalAllowed but it's negligible (IMHO).

As MisterSquonk suggested there is an Intent that you could listen for (ACCOUNTS_CHANGED_INTENT) but, unfortunately, it's broadcast when an account changes, and not just when an account is deleted.

I don't understand why this isn't part of the SDK but maybe we've both missed something obvious! For now, I'm sticking with this approach as I need to delete some database tables of my own on account deletion.

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消