Android 小部件在未显示时不会更新
我有一个可以倒计时的小部件。 它位于主屏幕上,当我停留在主屏幕上时它似乎工作正常, 但是当我切换到另一个屏幕一段时间然后返回主屏幕时,小部件开始滞后。
当我在其他屏幕上时,它似乎没有进行更新,因为时间没有更新,现在它正在一起执行所有更新来计算倒计时时间。这会导致延迟,并且手机会短暂冻结(当我每分钟调用更新时)。如果我想每秒更新一次 - 大约 20 分钟的屏幕锁定时间或其他失焦操作后,手机会完全冻结。
问题:我是否可以隐式告诉小部件继续更新,无论它是否显示在屏幕上?或者还有什么办法可以解决这个问题?
I have a widget that counts down time.
It's on the main Home screen and it seems to work fine while i'm staying on the Home screen,
BUT when i switch to another screen for a while and then get back to HOME, widget starts lagging.
It seems like it doesn't do updates while I was on other screen, cause the time isn't updated and now it's performing all updates all together calculating the countdown time. This causes lags, and short phone freezes (this when i call update each minute). In case I want to update each second - phone freezes totally after about 20 minutes of screen lock time or other out of focus action.
Question: can I implicitly tell widget to continue the updates regardless if it's shown on screen, or not? Or how else can this issue be solved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
View.OnFocusChangeListener
我猜您可以使用它来监视您的小部件是否处于焦点状态。在谷歌文档上查看一下。我自己还没有使用过它,所以我不确定它是否有效,但绝对值得一试。
View.OnFocusChangeListener
Im guessing you can use this to monitor whether or not your widget is in focus or not. Check it out on the google docs. I have yet to use it myself so Im not sure it would work but its definitely worth a try.
这是一个糟糕的整体设计 - 如果您必须计算每个“滴答声”并让它在那时发生,那么服务可能是完成这项工作的最佳选择,只需让小部件询问服务应该显示什么即可。
但是...你会消耗电池一些可怕的东西。它是否真的需要在每个刻度上执行某些操作,或者您可以只存储最后一次绘制时间,找到当前时间,然后进行相应更新,跳过其间的所有刻度?
如果在为零时需要发生某些事情,请尝试使用 Android 警报系统。如果采用不同的方式,您的用户的电池寿命和 CPU 将会变得更好。
It's a bad overall design - if you must count each "tick" and have it happen at that time, a service is probably the best thing to do the work, and just have the widget ask the service what it should display.
But... you're going to suck battery something awful. Does it really need to do something on each tick, or can you just store the last draw time, find the current time and then update accordingly, skipping all the ticks inbetween?
And if something needs to happen when it hits zero, try using the android Alarm system. You'll be so much nicer to your user's battery life and CPU doing it a different way.
我不确定你是否这样做,但你必须使用 AlarmManager 类来更新你的小部件。不要使用任何线程或可能导致延迟的东西。
我用过它,它对我有用。 查看此内容
I'm not sure your are doing that or not, but you have to use the AlarmManager class to update you widget. Don't use any threads or something that may cause the delay.
I used it and it worked for me. See this