Android - how update widget often but only when it is visible?

tomash picture tomash · Mar 10, 2010 · Viewed 12.3k times · Source

I'm going to create widget which needs to update its content every minute (it shows time-related data).

However, there is no need to update widget if it is currently invisible, which means:

  • screen is turned off
  • another app is running
  • widget is placed on another (invisible) home screen tab

What is the best way to update only visible widget every minute - without waking up device nor doing unnecessary computations? After widget becomes visible, small lag before update is acceptable.

Answer

Mark B picture Mark B · Mar 10, 2010

To keep from updating when the screen is off, use the AlarmManager to schedule a recurring alarm that doesn't wakeup the phone.

The other two bullet points you have in your question aren't possible. There is no way to detect if your widget is on a home screen that isn't currently visible, and there is no way to determine if an app is running that is hiding the home screen. I have filed a ticket on http://b.android.com requesting this functionality be added to Android. If you feel like starring it, it will help it gain priority: http://code.google.com/p/android/issues/detail?id=5529&q=reporter:mark.r.baird&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars