应用程序小部件 ID,但没有小部件
我目前正在为我的应用程序创建一些应用程序小部件;其中之一是包含时钟的小部件,需要每 60 秒更新一次。因此,我在 AppWidgetProvider 的 onEnabled() 函数中创建一个警报,该警报每 60 秒发送一次时钟更新意图,该意图由 appwidgetprovider 接收;这就像一个魅力,但是当没有更多的时钟小部件时,我想摆脱计时器,因为我喜欢保持整洁。因此,我删除了 AppWidgetProvider 的 onDisabled() 函数中的警报,因为当最后一个小部件类型被删除时会调用该警报。
然而问题是,即使我删除最后一个小部件,appwidgetmanager 中仍然保留有 widgetId,因此 onDisabled() 永远不会被调用。我尝试重新启动设备,但它们似乎仍然存在。
这是常见的行为吗?我不应该依赖 onDisabled() 被调用吗?这是一个错误,还是我做了一些非常错误的事情?
Im currently working on creating some appwidgets for my application; one of those is a widget containing a clock which needs an update every 60 seconds. I therefore create an alarm in the onEnabled() function of the AppWidgetProvider which sends a clock update intent every 60 seconds, which is received by the appwidgetprovider; this works like a charm, however id like to get rid of the timer when there are no more clock widgets as i like to keep things tidy. I therefore remove the alarm in the onDisabled() function of the AppWidgetProvider, as that one is called when the last of the widget type is removed.
The problem however is that even when I remove the last of the widgets, there are still widgetIds remaining in the appwidgetmanager and thus onDisabled() is never called. I tried rebooting the device, but they seem to persist.
Is this common behaviour and should i not rely on onDisabled() being called, is this a bug, or am i doing something terribly wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未能添加到主屏幕的 Android 1.6 及之前的小部件仍在列表中。我称它们为“幻影小部件”。当您尝试添加小部件但没有足够的空间时,它们就会被创建,并且您会收到有关此消息的消息。但该小部件仍以某种方式进入内部列表。我认为这个问题应该在 2.0 中修复,但我不确定。
除此之外,仅使用 onEnabled 和 onDisabled 对于您的目的来说是一个非常糟糕的主意。即使屏幕关闭,您的小部件也会继续更新。您的设备永远不会真正进入睡眠状态,并且电池电量会很快耗尽。至少添加一个 ScreenOn 和 ScreenOff 的接收器!
Up to and including Android 1.6 widgets which fail to get added to the home screen are still in the list. I call them "phantom widgets". They get created when you try to add a widget but there's not enough space and you will get a message about that. But the widget somehow still makes it to the internal list. I think this was supposed to be fixed with 2.0 but I'm not sure.
In addition to that, using only onEnabled and onDisabled is a very bad idea for your purpose. Even when the screen is switched off your widgets will continue to get updated. Your device will never really sleep and the battery will drain too quickly. At least add a Receiver for ScreenOn and ScreenOff!