java 如何安排我的 android 应用程序每小时做某事
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3696082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to schedule my android app to do something every hour
提问by Marqs
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.
我希望我的应用程序每小时访问一次数据库并从表中读取下一条记录,然后更新 desctop 小部件并发送通知。我知道有一个 AlarmManager,我可以用它来注册我的意图,但是当手机关闭或重新启动时它们会被删除。
Is there any other android class/service that I would update my application continuously even when I reboot my phone?
即使我重新启动手机,是否还有其他 android 类/服务可以持续更新我的应用程序?
Thanks,
谢谢,
采纳答案by Aaron Saunders
take a look at the demo applications provided with android sdk
看一下 android sdk 提供的演示应用程序
http://developer.android.com/samples/RepeatingAlarm/index.html
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
触发警报后查看 AlarmService_Service 以了解服务的实现
回答by mtmurdock
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.
将您想要执行的所有后台任务放在您的应用程序中Services
,其中在后台执行任务。在服务中,您应该能够定义一个计时器,它会导致您希望每 x 小时发生一次更新。
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 the Service
is always running.
在onCreate()
小部件的 中,启动服务。onCreate()
每次小部件出现时都会调用它(例如当手机在主屏幕上时),因此将保证Service
始终运行。
Hope this was helpful.
希望这是有帮助的。
回答by AlvaroSantisteban
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
http://developer.android.com/training/scheduling/alarms.html#boot