0

I'm writing an Android app widget that updates every minute with the current data. To update it, I'm using an AlarmManager that only works while the phone is not asleep:

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), INTERVAL_SEC * 1000, createFrequentUpdateIntent(context));

However, I'm facing the following challenge: when the phone wakes up after a long time, I want the user to see immediately that the data is obsolete because it hasn't been updated for hours. So I want to somehow mark the data as obsolete if there have been no updates for a long time.

I thought about hooking on the SCREEN_ON event, but apparently this is only possible through a background service and this is strongly discouraged (here and here).

Do you have any ideas of how to do this?

Thanks!

Community
  • 1
  • 1
Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141

1 Answers1

0

Perhaps in addition to the AlarmManager you could use the typical updateTimeMills update method, and have it called every 30m or so. When you get an update from this method, you could replace the widget with the "stale data" version of your layout, such that when the phone comes back on it's already been redrawn with the stale data notification.

In the event that this update triggers when your phone is on, you could just check for how recent the last update was, and then ignore it if the data is not stale.

I think that this would work because updateTimeMills update is called even when the phone is asleep, so it should still update when your AlarmManager is stopped.

Tim
  • 35,413
  • 11
  • 95
  • 121
  • `updateTimeMillis` on what object? Who will be responsible for running it after 30 minutes? – Ilya Kogan Nov 14 '12 at 14:18
  • Oh sorry, guess I wasn't clear. If you look at the [Android page on AppWidgets](http://developer.android.com/guide/topics/appwidgets/index.html), it explains that your AppWidgetProviderInfo metadata has a parameter called `android:updateTimeMills` which is how many milliseconds between successive updates. You can use this to get updates when the device is asleep. – Tim Nov 14 '12 at 15:03