如何安排我的 Android 应用程序每小时执行某项操作
我希望我的应用程序每小时访问数据库并从表中读取下一条记录,然后更新 desctop 小部件并发送通知。我知道有 AlarmManager,我可以用它来注册我的意图,但当手机关闭或重新启动时它们会被删除。
即使我重新启动手机,是否还有其他 Android 类/服务可以持续更新我的应用程序?
谢谢,
I want my app to access database every hour and read next record from the table then update desctop widget and send notification. I know that there is AlarmManager which I can use to register my Intents but they are deleted when the phone is turned off or rebooted.
Is there any other android class/service that I would update my application continuously even when I reboot my phone?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下 android sdk 提供的演示应用程序
http://developer.android.com /samples/RepeatingAlarm/index.html
查看 AlarmService_Service 以了解触发警报后服务的实现
take a look at the demo applications provided with android sdk
http://developer.android.com/samples/RepeatingAlarm/index.html
the look at AlarmService_Service for the implementation of the service once the alarm has been triggered
将您想要在应用中执行的所有后台任务放入在后台执行任务的
Services
中。在服务中,您应该能够定义一个计时器,该计时器会导致您希望每 x 小时发生一次更新。在小部件的
onCreate()
中,启动服务。每次小部件启动时都会调用onCreate()
(例如,当手机启动时,如果它位于主屏幕上),因此将保证Service
总是在运行。希望这有帮助。
put all of the background tasks you want to do in your app in
Services
which perform tasks in the background. In the service you should be able to define a timer that causes whatever updates you want to occur every x hours.In the
onCreate()
of the widget, start the service.onCreate()
is called every time that the widget comes to life (such as when the phone starts if it is on the home screen) and will therefore guarantee that theService
is always running.Hope this was helpful.
只是为了完整起见,我在这里链接了官方文档,其中解决了重新启动的问题。
http://developer.android.com/training/scheduling/alarms.html#boot
Just for the sake of completness, I am linking here the official docs, where the issue of the rebooting is adressed.
http://developer.android.com/training/scheduling/alarms.html#boot