Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 242
tomash Avatar asked Mar 10 '10 12:03

tomash


People also ask

How do you update widgets on Android?

Full update: Call AppWidgetManager. updateAppWidget(int, android. widget. RemoteViews) to fully update the widget.

Why do my widgets keep disappearing Android?

The most common reason for a widget to disappear is when Android users transfer applications to a memory card. Widgets may also disappear after the hard reboot of your device. To return it, you need to transfer them again to the phone's memory.

What happened to Android widgets?

Widgets are now in the Apps list. Open your app drawer and you'll see them. Some of the apps may not have ICS compatible apps. Just check for updates for your apps and see if that resolves it.


2 Answers

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

like image 114
Mark B Avatar answered Oct 03 '22 17:10

Mark B


  1. You can ask the PowerManager about isScreenOn()
  2. You can register for Intent ACTION_SCREEN_OFF/ACTION_SCREEN_ON and turn you timer on/off accordingly.

Allthough the answer above concerning the AlarmManager is correct it may not be sufficient since I observed many phones also delivering alarms even if they are not of type *_WAKEUP. This may happen if there are other applications installed that are waking up the device. And if it is once awake, it delivers all pending alarms.

like image 29
jom Avatar answered Oct 03 '22 17:10

jom