如何保存 Android 服务的数据?
我有一个收集数据的服务,我可以在重新启动或任何其他会重新启动该服务的操作时保存数据吗?我该怎么做?
I have a service that collects data, can I save the data in case of reboot or any other action that will restart the service, and how can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不一定会知道何时会发生此类事件。随时保存您的数据。
使用文件。或者数据库,它是一种文件类型。或者一个由某种类型的文件支持的 SharedPreferences 结构。
You will not necessarily find out when those sorts of events will occur. Save your data as you go.
Use a file. Or a database, which is a type of file. Or a
SharedPreferences
structure, which is backed by a type of file.您可能想要利用 Android 的 SQLite3 支持,它为您在手机上提供了一个轻量级关系数据库,您可以在其中存储数据。有关介绍(以及一些其他选项),请参阅:http:// /developer.android.com/guide/topics/data/data-storage.html
You may want to make use of Android's SQLite3 support, which provides you with a lightweight relational database on the handset where you can store your data. For an introduction (as well as some other options), see: http://developer.android.com/guide/topics/data/data-storage.html
或者使用序列化 API
http://java.sun.com/developer/technicalArticles/Programming /序列化/
Or use the serialization API
http://java.sun.com/developer/technicalArticles/Programming/serialization/
Android 服务有一个像其活动一样的生命周期。请参阅文档,了解在服务之前保存状态的正确回调正在关机。
我可以推荐 onDestroy() 方法吗?
Android Services have a lifecycle like its Activities. Refer to the documentation for proper callbacks to save state before a service is shutdown.
Might I recommend the onDestroy() method.