5

I have a widget that displays the current time and some other info, therefore it has to update once every minute. I am using an AlarmManager that triggers every minute and calls an IntentService that updates the widget. This solution works fine in Android versions prior to Oreo. On Oreo, this solution does not work - it looks like the AlarmManager is not firing. I have read that there is a TextClock control now that automatically updates the text to the current time but unfortunately this is not a solution since I need to draw other non-text information on the widget.

The alarm is set as follows:

if (android.os.Build.VERSION.SDK_INT >= 19) {
    alarmManager.setExact(AlarmManager.RTC, timeInMillis(), intent);

} else {
    alarmManager.setRepeating(AlarmManager.RTC, timeInMillis(), 1000 * 60, intent);
}

Note that the alarm is rescheduled every time it fires on android 19+ since it is not a repeating alarm.

Is there an alternative solution for the above to work in Android O and later?

a.p.
  • 3,248
  • 7
  • 30
  • 48
  • 1
    What you want is not practical on newer versions of Android, as Google continues to try to improve battery life. Since the user already has the current time on their home screen (in the status bar), the simplest and most battery-friendly approach is to get rid of time from your app widget, then update that app widget much less frequently. – CommonsWare May 18 '18 at 10:45
  • I understand, but this has been working for several years and no battery issues were reported or detected. This is an app that has a lot of users and the main reason they are using the widget is for the display of the time (the time is displayed in large text using different fonts that the user can choose). Does this mean that Android O is the end of clock widgets? – a.p. May 18 '18 at 13:14
  • 1
    "Does this mean that Android O is the end of clock widgets?" -- I have no idea how you would implement one that would update every minute on Android 8.0+. – CommonsWare May 18 '18 at 13:17
  • Hi, Have you fixed this issue? – Harish Kamboj Aug 18 '18 at 06:58
  • Unfortunately it is not possible to do this in Android O or newer. You could use workarounds to update the widget every minute (JobScheduler, etc) but once the screen turns off no one can guarantee that the widget will update every minute. – a.p. Aug 20 '18 at 06:25
  • Why not come on....we still love widgets... – stuckedunderflow Jan 12 '19 at 15:53
  • I saw somebody makes it works using JobIntentService. Maybe you can try that! – stuckedunderflow Jan 12 '19 at 16:48
  • We have already tried that but unfortunately you cannot rely on this getting executed every minute; most of the times it works but sometimes not. – a.p. Jan 14 '19 at 06:43

1 Answers1

0

If you are using simple TextViews to display the Clock, use a TextClock instead of TextView (added in API Level 17)

If you are using more complex Views (e.g. you draw a bitmap), you have to use a foreground service, which either uses explicit TIME_TICK receivers or Handlers to schedule the updates. You have to show a foreground notification and if not properly implemented, it could drain a lot of battery...

metinkale38
  • 718
  • 3
  • 10
  • 28