自定义帐户验证器。从设备中删除帐户后的清理

发布于 2024-11-09 10:02:40 字数 299 浏览 0 评论 0原文

有没有办法获得某种通知/广播/等。当自定义帐户从“帐户和同步设置”中删除时?

我拥有的应用程序可以方便设备上的多个用户(这是供公司使用)并使用单个 SQLite 数据库。假设我在设备上为我的应用程序创建多个用户,并使用仅与这两个用户相关的数据填充数据库。我的问题是,如果其中一个用户从“帐户和同步设置”中删除,我将无法清理 SD 卡上的数据库和/或某些外部文件。

我可以在冗余表中复制用户信息,并将其与注册帐户进行比较,然后如果表中的用户信息与 AccountManager 中的 Account[] 数组不匹配,则从数据库中删除用户数据。我感觉很脏。

Is there a way to get some kind of notification/broadcast/etc. when a custom account is removed from "Accounts & sync settings"?

The application I have can facilitate multiple users on a device (this is for a corporate use) and uses a single SQLite database. Say I create multiple users for my application on a device and populate database with data that is relevant only to those two users. My problem here is that if one of the user is removed from "Accounts & sync settings" I have no way to cleanup database and/or some external files on SD card.

I could duplicate user information in a redundant table and compare it with registered accounts and then removing user data from the database if user information in the table and Account[] array from AccountManager does not match. Feels dirty to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋顶上的小猫咪 2024-11-16 10:02:40

您有两个选择:

  1. 您可以使用addOnAccountsUpdatedListener 方法AccountManagerActivityServiceonCreate 方法中添加侦听器 - 确保删除侦听器在您的 onDestroy 方法中(即不要在无限运行的服务中使用此方法),或者用于检索 AccountManagerContext 永远不会被垃圾收集

  2. AccountsService 将通过操作 广播意图<一href="http://developer.android.com/reference/android/accounts/AccountManager.html#LOGIN_ACCOUNTS_CHANGED_ACTION" rel="noreferrer">AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION 每次添加、删除帐户时或更改您可以为其添加接收器的位置。

You have two options:

  1. You can use the addOnAccountsUpdatedListener method of AccountManager to add a listener in the onCreate method of an Activity or Service -- make sure you remove the listener in your onDestroy method (i.e. do NOT use this in an endlessly running service) or the Context used to retrieve the AccountManager will never be garbage collected

  2. The AccountsService will broadcast an intent with the action AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION every time an account is added, removed or changed which you can add a receiver for.

倾其所爱 2024-11-16 10:02:40

我没有看到很多关于人们如何实施帐户清理的示例,所以我想我会发布我的解决方案(实际上是已接受答案的变体)。

public class AccountAuthenticatorService extends Service {
    private AccountManager _accountManager;
    private Account[] _currentAccounts;
    private OnAccountsUpdateListener _accountsUpdateListener = new OnAccountsUpdateListener() {
        @Override
        public void onAccountsUpdated(Account[] accounts) {

            // NOTE: this is every account on the device (you may want to filter by type)
            if(_currentAccounts == null){
                _currentAccounts = accounts;
                return;
            }

            for(Account currentAccount : _currentAccounts) {
                boolean accountExists = false;
                for (Account account : accounts) {
                    if(account.equals(currentAccount)){
                        accountExists = true;
                        break;
                    }
                }

                if(!accountExists){
                    // Take actions to clean up.  Maybe send intent on Local Broadcast reciever
                }
            }
        }
    };

    public AccountAuthenticatorService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        _accountManager = AccountManager.get(this);

        // set to true so we get the current list of accounts right away.
        _accountManager.addOnAccountsUpdatedListener(_accountsUpdateListener, new Handler(), true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
       _accountManager.removeOnAccountsUpdatedListener(_accountsUpdateListener);
    }

    @Override
    public IBinder onBind(Intent intent) {
        AccountAuthenticator authenticator = new AccountAuthenticator(this);
        return authenticator.getIBinder();
    }
}

I didn't see a lot of examples on how people implement account cleanup, so I thought I would post my solution (really a variation of the accepted answer).

public class AccountAuthenticatorService extends Service {
    private AccountManager _accountManager;
    private Account[] _currentAccounts;
    private OnAccountsUpdateListener _accountsUpdateListener = new OnAccountsUpdateListener() {
        @Override
        public void onAccountsUpdated(Account[] accounts) {

            // NOTE: this is every account on the device (you may want to filter by type)
            if(_currentAccounts == null){
                _currentAccounts = accounts;
                return;
            }

            for(Account currentAccount : _currentAccounts) {
                boolean accountExists = false;
                for (Account account : accounts) {
                    if(account.equals(currentAccount)){
                        accountExists = true;
                        break;
                    }
                }

                if(!accountExists){
                    // Take actions to clean up.  Maybe send intent on Local Broadcast reciever
                }
            }
        }
    };

    public AccountAuthenticatorService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        _accountManager = AccountManager.get(this);

        // set to true so we get the current list of accounts right away.
        _accountManager.addOnAccountsUpdatedListener(_accountsUpdateListener, new Handler(), true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
       _accountManager.removeOnAccountsUpdatedListener(_accountsUpdateListener);
    }

    @Override
    public IBinder onBind(Intent intent) {
        AccountAuthenticator authenticator = new AccountAuthenticator(this);
        return authenticator.getIBinder();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文