0

Well, i've interesting problem. I have a widget, something like reminder, which shows few nearest items on screen. Each item time, when it comes. I need to show only items, which are in the future, not passed items. As regular update can be done at least every 30 minutes, its not enough for me (my items has tiems like 11.54, 12.07, etc). So i have only 1 option = schedule an service, which can update screen in any interval i want (for example every 5 minute).

But this is not very good for me, because i have 2 requirements, which are a little bit fighting together:

  • run very often (<5 minutes)
  • do not use too much of battery and other resources

So - is it possible something like intelligent update? I mean for example algorithm like:

a) update screen every 1 minute when device is unlocked (=user is doing something with phone) b) but do not update when device is sleeping (its useless, as user can't see result of update)?

Or at least something like "run every 5 minutes, but only from 8:00am to 8:00pm during working days"? Simple intelligent service scheduling...

qkx
  • 2,383
  • 5
  • 28
  • 50

1 Answers1

0

So i have only 1 option = schedule an service, which can update screen in any interval i want (for example every 5 minute).

There are other, more efficient options, like AlarmManager.

is it possible something like intelligent update? I mean for example algorithm like

An intelligent algorithm would recognize the fact that you only need to update the app widget when the list changes, not every minute, second, or millisecond.

Use AlarmManager to set an alarm for one minute past the first item on your list. When you get control from the alarm event, update the app widget, then use AlarmManager to set an alarm for one minute past the next time on your list. Also update your app widget when the list changes from other sources (e.g., UI, sync process), and if that might cause you to need an earlier alarm, cancel your current alarm and schedule a new one using AlarmManager.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • yeah, of course i'm implementing this right now ;) I just wanted to know, if there are "more simple" options then do it by my own. Well, another option which was looking promisingly was listening to event SCREEN_ON and SCREEN_OFF - it should be absolutely best option for battery. But however, for some strange reasons my broadcast receiver can't listen for this 2 types of intents. Then i found your reply to this thread http://stackoverflow.com/questions/2575242/android-intent-action-screen-on-doesnt-work-as-a-receiver-intent-filter, and realized, that not always everything is 100% documented ;) – qkx Jun 21 '12 at 11:11
  • @qkx: "Well, another option which was looking promisingly was listening to event SCREEN_ON and SCREEN_OFF - it should be absolutely best option for battery." -- not really. After all, you do not want to force the user to turn their screen off and on just to update your list, which would mean you would need another trigger *in addition to* the screen status change. Using `AlarmManager` with a non-`_WAKEUP` alarm will also avoid unnecessary updates while the device is asleep, while simultaneously handling your when-the-screen-is-on updates in the most efficient manner possible. – CommonsWare Jun 21 '12 at 11:34
  • i mean, then I would have a service running in regular itnerval (alarmmanager) for example 5 min and when screen is off i will cancel this alarm, so service wont be running even 1 time when the screen is off. After screen is on, i wil recreate alarm and schedule service to run in same interval, and so on. But it doesn't matter, it don't work so i will implement my own algorithm...question closed ;) – qkx Jun 21 '12 at 12:09