android中经常检查数据库
我需要创建一个 Android 应用程序,将事件和提醒保存在数据库中。当正确的时间到来时,它应该显示一条通知。对于这种情况,我需要创建一个服务来查找任何特定时间的事件。我应该用什么方法来做这样的事情?
提前致谢 !!!
I need to create an android app which save events and reminders in a database. When the correct time comes it should show a notification. For that case I need to create a service to look for events for any particular time. What is the method I should use to do such a thing ?
Thanks in Advance !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
AlarmManager
安排在第一个事件发生时获取控制权。那时,您可以做任何您需要做的事情(例如,显示通知
来提醒用户),并为下一个事件安排下一个警报。Use
AlarmManager
to arrange to get control when the first event occurs. At that time, you can do whatever it is you need to do (e.g., display aNotification
to remind the user) plus schedule the next alarm for the next event.我所做的是创建一个
Service
并让它进行轮询,当它发现某些内容时,它会调用Activity
来显示通知。对于服务,我只使用一个调用自身的线程,然后延迟调用处理程序。
我选择了一项服务,以便它可以在后台运行,因为这似乎比保持可能被杀死的活动更好。
我让用户可以在几分钟内配置延迟。
What I did was to create a
Service
and have it do the polling, and when it finds something it then calls anActivity
to display the notification.For the Service I just use a thread that calls itself, and then has a delayed call to a handler.
I went with a service so it could run in the background, as it seems to be better than keeping an activity alive that may be killed off.
I had the delay be user-configurable in minutes.