如何安排我的 Android 应用程序每小时执行某项操作

发布于 2024-09-18 15:01:33 字数 171 浏览 5 评论 0原文

我希望我的应用程序每小时访问数据库并从表中读取下一条记录,然后更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱娆 2024-09-25 15:01:33

看一下 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

深海夜未眠 2024-09-25 15:01:33

将您想要在应用中执行的所有后台任务放入在后台执行任务的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 the Service is always running.

Hope this was helpful.

云淡风轻 2024-09-25 15:01:33

只是为了完整起见,我在这里链接了官方文档,其中解决了重新启动的问题。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文