自定义帐户验证器。从设备中删除帐户后的清理
有没有办法获得某种通知/广播/等。当自定义帐户从“帐户和同步设置”中删除时?
我拥有的应用程序可以方便设备上的多个用户(这是供公司使用)并使用单个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两个选择:
您可以使用
addOnAccountsUpdatedListener
方法AccountManager
在Activity
或Service
的onCreate
方法中添加侦听器 - 确保删除侦听器在您的onDestroy
方法中(即不要在无限运行的服务中使用此方法),或者用于检索AccountManager
的Context
永远不会被垃圾收集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:
You can use the
addOnAccountsUpdatedListener
method ofAccountManager
to add a listener in theonCreate
method of anActivity
orService
-- make sure you remove the listener in youronDestroy
method (i.e. do NOT use this in an endlessly running service) or theContext
used to retrieve theAccountManager
will never be garbage collectedThe
AccountsService
will broadcast an intent with the actionAccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION
every time an account is added, removed or changed which you can add a receiver for.我没有看到很多关于人们如何实施帐户清理的示例,所以我想我会发布我的解决方案(实际上是已接受答案的变体)。
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).