AlarmService onBoot 的间隔基于变量
好吧,我正在开发一个项目,我希望应用程序检查互联网是否有更新。我还希望用户能够自定义进行这些检查的频率。
我有点纠结如何去做这件事。 我想我有一个 BroadcastReceiver 检查 Intent.ACTION_BOOT_COMPLETED,然后根据已在某处设置的变量启动 AlarmService。
但是如果用户想要更新间隔会怎样呢?如何停止旧的 AlarmService 并将其替换为新的?
另外,如何让我的 AlarmService 在我的应用程序中运行“后台更新”部分,而不实际运行任何活动?
编辑:如果我正确使用 AlarmService,有人可以建议吗?我想经常检查更新,我应该使用其他方法吗?我会检查 1-5 分钟。
Alright so I'm working on a project where I want the app to check the internet for updates. I also want the user to be able to customize how often it makes these checks.
I'm kinda struggling with how I go about doing this.
I'm thinking I have a BroadcastReceiver check for the Intent.ACTION_BOOT_COMPLETED, and then start the AlarmService based upon a variable already set somewhere.
But what happens if the user wants to update the interval? How do I stop the old AlarmService and replace it with the new one?
Also how do I have my AlarmService run a "background update" portion in my app without actually running any of the activities?
EDIT: Also can someone advise if I'm using the AlarmService correctly? I'd want to check for updates fairly frequently, should I be using some other method? I'd check between 1-5 minutes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在我的应用程序中做了类似的事情。我基本上有两种服务:一种用于设置警报,另一种用于实际下载。
当您的 BroadcastReceiver 获得意图 ACTION_BOOT_COMPLETED 时,您将启动 AlarmService,该服务会发送广播以启动 downloadService。
所以你的BroadcastReceiver会做两件事:如果intent == BOOT_COMPLETED则启动alarmServcie,否则启动下载服务。
此外,如果用户更改了他希望完成这些更新的时间段,您可以将所选值存储在首选项中,启动警报服务并检索值以设置新的时间段,例如 1 小时、2 小时
编辑:这是info 文档中有关待处理意图以及如何取消它们的一些信息。 http://developer.android.com/reference/android/app/PendingIntent.html
当用户更改间隔时,您应该首先取消旧的警报服务,然后重新设置。
I do a similar thing in my app. I basically have two services: one that sets the alarm and the other that does the actual donwloading.
When your BroadcastReceiver get's the intent ACTION_BOOT_COMPLETED you start your alarmService which sends an broadcast to start the downloadService.
So your BroadcastReceiver does both things: starts the alarmServcie if intent == BOOT_COMPLETED else it starts the downloadservice.
Also if the user changes the periods in which he wants those updates to be done you store the chosen value in the preferences, start the alarmservice and retrieve the values to set up a new period, e. g. 1 hour, 2 hours
Edit: Here is the info some info from the docs about pending Intents and how to cancel them. http://developer.android.com/reference/android/app/PendingIntent.html
When the user changes the interval you should first cancel your old alarmservice and then set it again.