安排 AlarmManager 进行定期后台数据同步的最佳位置在哪里?
我的 Android 应用程序将定期轮询服务器以检查数据。我希望无论用户与应用程序的交互如何,都会发生这种轮询,类似于(在概念上)Gmail 和 Google Reader 应用程序在后台同步数据的方式。安装应用程序后,这些定期同步就会开始发生。我不认为从Activity
内部安排警报是可行的方法,因为我不想等待用户打开我的应用程序。
在这种情况下,调用AlarmManager.setInexactRepeating
的最佳实践是什么?
我能想到的一些可能性是:
- 扩展
Application
并在onCreate
中执行 - 从设置为
的
。Service
中进行调度清单中的 android:enabled=true - 侦听某些特定的广播消息,并从接收器内进行调度
My Android application will be periodically polling a server to check for data. I want this polling to happen regardless of user interaction with the application, similar (in concept) to how the Gmail and Google Reader apps sync data in the background. Once the app is installed, these periodic syncs should begin happening. I do not think that scheduling the alarm from inside an Activity
is the way to go, because I do not want to wait for the user to open my application.
What is the best practice in this case for placing the call to AlarmManager.setInexactRepeating
?
Some possibilities I could think of are:
- Extend
Application
and do it inonCreate
- Do the scheduling from within a
Service
that is set toandroid:enabled=true
within the manifest. - Listen for some particular broadcast message(s), and schedule from within a receiver
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个启动接收器并使用它来启动安排警报的服务。查看此链接:http://www.thetekblog.com/?p=77
Create a on boot receiver and use it to start a service that schedules the alarm. Check out this link: http://www.thetekblog.com/?p=77
“在清单中设置为 android:enabled=true 的服务中进行调度。”
默认情况下启用服务 - 但这并不意味着任何东西都会自动启动它。在配置中禁用服务的唯一原因是您在某处有一些代码可以以编程方式启用它。
"Do the scheduling from within a Service that is set to android:enabled=true within the manifest."
A service is enabled by default - but that doesn't mean anything would start it automatically. The only reason a service would be disabled in configuration is if you have some code somewhere that enables it programmatically.