Android 偏好设置启动服务
我正在构建一个应用程序,它应该只有一个带有三个复选框的屏幕和一个用于保存用户对复选框的选择的按钮。
如果用户勾选复选框并单击按钮,则即使应用程序关闭,也应该启动并运行某个服务 - 直到应用程序再次启动并且用户取消勾选复选框并再次单击按钮。当然,如果用户取消选中该复选框,我希望下次启动应用程序时,该复选框保持未选中状态,反之亦然。
所以我的问题是,我可以采取什么样的策略来解决这个问题?正常的偏好活动?共享首选项?
谢谢大家提前的回答:-)
I am building an app which should only have a screen with three checkboxes and a button to save the users' choices about the checkboxes.
In case the user ticks a checkbox and clicks the button, then a certain Service should be started and running even if the app is closed -until the app is started again and the user unticks the checkbox and clicks the button again. Of course, if the user unticks the checkbox, I want that next time the app is started, the checkbox remains unticked, and viceversa.
So my question is, what kind of strategy could I address this? Normal PreferenceActivity? SharedPreferences?
Thanks everybody for the answers in advance :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用 SharedPreferences 来实现类似的目的,只是因为这是让我的服务和活动查看信息的简单方法。
因此,只要有一个 RemoteService (http://saigeethamn. blogspot.com/2009/09/android-developer-tutorial-part-9.html)和一个活动。
Activity启动起来查看信息,就可以启动服务了。如果用户取消选中该框,则可以停止服务。
因此,服务始终在需要时运行,并且不与 Activity 绑定,因此当 Activity 终止时,服务会继续运行。
I used
SharedPreferences
for something similar to this, simply because it was an easy way to have my Service and Activities see the information.So, just have a RemoteService (http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-part-9.html) and an Activity.
The Activity starts up and looks at the information, and can start up the service. If the user unchecks the box then it can stop the service.
So, the service is always running when desired, and is not tied to the Activity, so when the Activity dies the service continues to run.