Android:应用程序终止时刷新 sqlite 数据库
我正在开发一个在 SQLite 数据库中存储一些信息的应用程序。
我的问题是,当应用程序被终止或手机重新启动时,我想清除数据库中的所有数据。 有谁知道我可以这样做吗?
谢谢 马可
I'm working on an app that stores some information inside an SQLite DB.
My problem is that I'd like to clear all data from the database when the app gets killed or the phone restarted.
Does anybody know I can I do this?
Thanks
Marco
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能没有一种完全万无一失的方法可以做到这一点。您可以清除活动的 onDestroy() 中的数据,但如果有人使用任务杀手之类的东西(对此非常确定),它可能不会被调用。
更好的选择是下次启动应用程序时清除数据。
There probably isn't a totally foolproof way of doing this. You could clear the data in onDestroy() of your activity, though it might not get called if someone uses something like a task killer (pretty sure about this).
A better option would be to clear the data the next time you start the app.
您应该看看 此方法子类化应用程序并在那里执行表删除/创建,而不是在 onStop 和 onPause 中。
You should take a look at this method of subclassing Application and performing your table dropping/creating there rather than in onStop and onPause.
我认为您应该通过传递 null 作为数据库名称来使用内存中标志。这意味着数据库根本不会存储在 SD 卡上,因此无需在应用程序终止之前清除它。
I think you should use the in-memory flag by passing null as your db name. That means the db won't be stored on sd card at all so no need to clear it before the app dies.