Updating a widget only when device is on AND widge

2020-06-17 04:59发布

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???

1条回答
一纸荒年 Trace。
2楼-- · 2020-06-17 05:54

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.

查看更多
登录 后发表回答