简体   繁体   English

检测到一个 android 账号被删除

[英]detecting the deletion of an android account

i'm developing an android application with accounts, sync and content provider.我正在开发一个带有帐户、同步和内容提供程序的 android 应用程序。 adding an account works, syncing also and some data is saved in the content provider.添加一个帐户有效,同步也和一些数据保存在内容提供者中。

now, when the user deletes the account using the settings, syncing stops, but the data stays in the content provider.现在,当用户使用设置删除帐户时,同步停止,但数据保留在内容提供者中。

i'd like to delete it, but i don't know how to catch the event of account deletion.我想删除它,但我不知道如何捕捉帐户删除事件。

There is AccountManager.addOnAccountsUpdatedListener() , i've tried to add it to the sync service, but the sync service is started only for the sync and then stopped.AccountManager.addOnAccountsUpdatedListener() ,我试图将它添加到同步服务,但同步服务仅为同步而启动,然后停止。 so whenever the account gets deleted while there is no sync, it can't get caught.因此,只要帐户在没有同步的情况下被删除,就无法被捕获。

is there a best practice on how to handle private data when an account gets deleted?是否有关于如何在帐户被删除时处理私人数据的最佳做法?

First You should add OnAccountsUpdateListener to you AccountManager using this command:首先,您应该使用以下命令将 OnAccountsUpdateListener 添加到您的 AccountManager 中:

AccountManager mAccountMgr = AccountManager.get(getContext());
mAccountMgr.addOnAccountsUpdatedListener(new AccountsUpdateListener(), null, false);

AccountsUpdateListener is implemented class of OnAccountsUpdateListener like this: AccountsUpdateListener 是这样实现的 OnAccountsUpdateListener class:

private class AccountsUpdateListener implements OnAccountsUpdateListener {
    @Override
    public void onAccountsUpdated(Account[] accounts) {
        Account newAccount = null;
        for (final Account account : accounts) {
            if (account.type.equals(mAccountType)) {
                newAccount = account;
            }
        }

        if (newAccount == null) {
          // account removed, now you can handle your private data and remove anything you want here
        }
    }
}

onAccountsUpdated fired when you add an account or an account removed. onAccountsUpdated 在您添加帐户或删除帐户时触发。 so you can check your account type to find specified account in accounts array.因此您可以检查您的帐户类型以在帐户数组中找到指定的帐户。 if it doesn't exist it removed.如果不存在,则将其删除。 mAccountType is your account type. mAccountType 是您的帐户类型。 eg mAccountType = "your application name"例如 mAccountType = "您的应用程序名称"

You should use method getAccountRemovalAllowed from your AbstactAccountAuthenticator :您应该使用AbstactAccountAuthenticator中的方法getAccountRemovalAllowed

    class AccountAuthenticatorImpl(context: Context) : AbstractAccountAuthenticator(context) {
    
        override fun getAccountRemovalAllowed(
            response: AccountAuthenticatorResponse?,
            account: Account?
        ): Bundle {
            val result = super.getAccountRemovalAllowed(response, account)
            val canDelete = result.getBoolean(android.accounts.AccountManager.KEY_BOOLEAN_RESULT, false)
    
            if (canDelete) {
                // TODO: account was deleted, so react on it
                accountRepository.onAccountDeletedInSystem()
            }
    
            return result
        }
    
    
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM