清除帐户删除数据

mgv*_*mgv 14 android

我想在用户从Accounts & sync设置应用中的部分手动删除帐户时清除应用程序的数据.

我有自己的实现,AbstractAccountAuthenticator但没有方法可以挂钩删除帐户进程.任何提示?

小智 18

我一直在思考同样的问题,这就是我决定采用的"解决方案".这不是我称之为"正确"的解决方案,但它是我认为您可以使用当前API管理的最佳解决方案.

在我的AbstractAccountAuthenticator类的实现中,我已经覆盖了getAccountRemovalAllowed如下函数:

@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;
}
Run Code Online (Sandbox Code Playgroud)

你返回后移除失败的可能性微乎其微,getAccountRemovalAllowed但可以忽略不计(恕我直言).

正如MisterSquonk所说,有一个Intent可以监听(ACCOUNTS_CHANGED_INTENT),但不幸的是,它会在帐户更改时进行广播,而不仅仅是在删除帐户时.

我不明白为什么这不是SDK的一部分,但也许我们都错过了一些明显的东西!现在,我坚持使用这种方法,因为我需要在删除帐户时删除我自己的一些数据库表.

我希望这有帮助.