清除帐户删除数据

时间:2011-04-21 18:33:50

标签: android

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

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

1 个答案:

答案 0 :(得分: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;
}

getAccountRemovalAllowed返回后移除可能会失败的可能性很小,但可以忽略不计(恕我直言)。

正如MisterSquonk所说,有一个你可以收听的意图(ACCOUNTS_CHANGED_INTENT),但不幸的是,当一个帐户更改时,它会被广播,而不仅仅是当一个帐户时删除

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

我希望这会有所帮助。