在多活动aplication实施WakefulIntentService [英] Implementing a WakefulIntentService in a multiple activity aplication

查看:241
本文介绍了在多活动aplication实施WakefulIntentService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要查询的频繁数据的应用程序。我与的TimerTask 执行一个应用程序的业务逻辑但后来我搬到的 WakefulIntentService 通过@Commonsware。

现在我的问题是,我有一个由服务发送一个广播意图做出回应多个活动画面。我如何确保对scheduleAlarms的通话将被一次所谓的(或者是没有必要的,我操心呢?)。实际的问题是,code到scheduleAlarms被置于从最如果不是所有的活动延长,从而导致预定的多个报警超类上的onCreate。


解决方案

  

我有需要查询的频繁数据的应用程序。我曾与TimerTask的执行一个应用程序的业务逻辑,但后来不@Commonsware搬到WakefulIntentService。


WakefulIntentService 是专为我会认为是罕见的间隔时间(每隔几分钟的最大值),从任何UI运行脱钩。这听起来就像是你是不是如何使用它。


  

我如何确保对scheduleAlarms的通话将被一次名为


跟踪,当你打电话吧。


  

或者是没有必要的,我操心呢?


这要看日程安排一下。

有关,你的目标是特定时间(比方说,下午4点下周二)报警时,可以重新安排盲目他们,因为下午4点在下周二没有改变。如果您使用的是等效的的PendingIntent 每一次,Android将取消旧报警用新替换它的过程。

有关定期报警(每n分钟/小时/天),你可以只是盲目地重新计划,有一点需要注意:除非你照顾,以避免你的时间表将有所转变。比方说,你想要一个闹钟每天一次熄灭。上次报警12小时后,您重新安排闹​​钟每天再度熄灭。如果你想确保报警还在另外12个小时(坚持你原来的时间表)熄灭,你需要知道,这是必需的,在你的 setRepeating设置初始事件( )调用合适的时间。

I have a application which needs to poll data at frequent intervals. I had an App Service logic with TimerTask implementation but then I moved to WakefulIntentService by @Commonsware.

Now my problem is that I have multiple activity screens that respond to a broadcast intent sent out by the Service. How do I ensure that the call to scheduleAlarms will be called only once (or is it not necessary that I bother about this?). The actual problem is that the code to scheduleAlarms is placed on a onCreate of a super class from which most if not all of the activities extend, thus resulting in multiple alarms scheduled.

解决方案

I have a application which needs to poll data at frequent intervals. I had an App Service logic with TimerTask implementation but then I moved to WakefulIntentService by @Commonsware.

WakefulIntentService is designed for what I would consider to be infrequent intervals (max of every few minutes), running decoupled from any UI. It sounds like that is not how you are using it.

How do I ensure that the call to scheduleAlarms will be called only once

Keep track of when you call it.

or is it not necessary that I bother about this?

That depends a bit on the schedule.

For alarms where you are aiming for particular times (say, 4pm next Tuesday), you can blindly reschedule them, because 4pm next Tuesday is not changing. If you are using an equivalent PendingIntent each time, Android will cancel the old alarm in the process of replacing it with the new one.

For periodic alarms (every N minutes/hours/days), you can just blindly reschedule them, with one caveat: your schedule will shift slightly unless you take care to avoid it. Let's say that you want an alarm to go off once per day. 12 hours after the last alarm, you reschedule the alarm to go off once per day again. If you want to make sure that the alarm still goes off in another 12 hours (to stick to your original schedule), you will need to know that this is required and to set the initial event in your setRepeating() call to the right time.

这篇关于在多活动aplication实施WakefulIntentService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆