Updating a widget only when device is on AND widget is visible on home screen
Asked Answered
M

1

7

As far as I understood the official documentation about Widgets I am a little bit confused.

The "standard" way would be to define a BroadcastReceiver and define the update frequency in milliseconds - this way allows a minimum update interval of 30 minutes to save battery power. Sounds reasonable.

The "manual" way I found reading the Google documentation would be to handle the update of widgets yourself using the AlarmManager. Using this method higher update intervals are possible (though of course discouraged!) and it would be possible to use alarm types that do not wake up the device when asleep. (I assume the intent is sent any time after the event should trigger as soon as the device wakes up for some other reason - is that right??)

Now, I get confused by the following thought: widgets reside on home screens. Or at least that's the most common place they will be found. Android offers many pages (standard is 5 or 7) on home screens, so a Widget, even though placed there by the user, might not even be visible!

As long as the information a widget displays is just a very simple text (like the current time!) that could be calculated or derived without any delay whenever needed, I do not want any update frequency or even device wake-up intents when it is not visible!
But as long as the device is on, or more specific: as long as the screen is on AND the widget is visible, I want a lot of update events! Like a clock displaying the seconds needs an intent to redraw every second!

How can that be realized? How is the "Android analog clock" realized? Neither method A ("standard") nor B ("manual") give my high update frequency but only as long as the device is on and my widget is visible.

Can anybody give me a hint???

Marilynnmarimba answered 22/2, 2011 at 10:11 Comment(0)
S
3

I assume the intent is sent any time after the event should trigger as soon as the device wakes up for some other reason - is that right??

Yes.

I do not want any update frequency or even device wake-up intents when it is not visible!

But your users do, in many cases. Anything you do that is worth doing in the update may take some number of seconds. The user is not going to sit there waiting some number of seconds. Hence, the app widget update model says that you keep the app widget updated constantly, so when the user goes looking at the app widget, they have the updated data right away.

But as long as the device is on, or more specific: as long as the screen is on AND the widget is visible, I want a lot of update events! Like a clock displaying the seconds needs an intent to redraw every second!

Write your own home screen app.

How is the "Android analog clock" realized?

It is a feature of the home screen app.

Schwa answered 22/2, 2011 at 13:3 Comment(2)
Trust me: my "calculation" does not consume ANY time. Displaying the minutes left to a point in time is nothing else than subtracting one every minute... :-) That's why I'm sure I don't need to do anything unless my widget is visible to the user! The clock widgets that I can add to the home screen are NO WIDGETS at all? Are you sure??Marilynnmarimba
@Zordid: "The clock widgets that I can add to the home screen are NO WIDGETS at all?" -- the ones that ship on your phone are features of the home screen. Just because they show up in a "Widgets" context menu does not mean that they are app widgets per the SDK.Schwa

© 2022 - 2024 — McMap. All rights reserved.