如何在偏好更改时重新安排警报管理器
我有一个 Android 服务。当手机启动时,广播接收器会收到通知,并安排服务以 X 分钟的间隔重复运行。从此以后,每隔 X 分钟,另一个广播接收器就会收到这些通知并启动该服务,该服务完成其工作并退出。到目前为止,一切都很好。
现在我希望这 X 分钟是可配置的。我有一个类似于下面链接中给出的 perf.xml 。该 XML 捕获了我的所有偏好以及服务计时器的偏好。 http://android- trip.blogspot.com/2010/01/for-almost-any-application-we-need-to.html
现在,当用户更改首选项时;如何重新安排闹钟时间?有没有办法可以设置偏好更改的监听器?我仅使用 XML 文件来指定首选项屏幕。
任何想法都会受到欢迎。
干杯
I have an Android Service. When a phone boots up, a broadcast receiver receives a notification and it schedules the service to run repeatedly at a gap of X minutes. Henceforth After every X minutes another broadcast receiver gets those notifications and kicks the service off, which does it's job and quits. So far so good.
Now I want those X minutes to be configurable. I have a perf.xml similar to what is given at the link below. This XML captures all my preferences along with that of service timer.
http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html
Now when user changes preferences; how can I reschedule the alarm? Is there a way I can set a listener on preferences change? I have used only XML files to speficy preferences screen.
Any ideas will be welcome.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有OnPreferenceChangeListener只需设置它并实现它。我记得你将其设置为首选项。因此,您需要获取此首选项 findPreference("myAlarmInterval");然后设置侦听器
如果您的服务不使用相同的首选项,这将起作用。根据开发人员文档,它将在以下情况下被调用:
- 已被用户更改
- 即将被设置和/或保留。
因此,新的首选项值尚不可用于您的服务。
There is OnPreferenceChangeListener just set it and implement it. As i remember you set it to the preference. So you need to get this preference findPreference("myAlarmInterval"); and then set the listener
This will work if your service doesn't use the same preference. According to the developer docs it will be called when
- has been changed by the user
- is about to be set and/or persisted.
So, a new preference's value is not yet available to your service.