检测最后一个被删除的小部件
我创建了一个使用后台服务进行更新通知的小部件。
如果从主屏幕中删除最后一个小部件,我想停止后台服务。
如何检测主屏幕中剩余的小部件数量?
I created a widget which uses a background service for update notifications.
I want to stop the background service if the last widget is removed from the home screen(s).
How can I detect number of widgets remaining in home screen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,我找到了一种不需要存储任何持久信息的方法。然而,即使这对我来说也像是一个黑客。在 onReceive 中插入以下内容 -
它有效,并且是我能找到的最佳解决方案,但我对此不太满意。
我需要在
onReceive
而不是onDeleted
中执行此操作,因为intent
对象需要执行intent.getComponent().我不喜欢这一事实,因为文档声称人们很少需要接触
onReceive
,并且需要计算剩余的小部件似乎是onDeleted
中的合理期望。然而不幸的是,它提供了被删除的小部件的计数,而不是剩余的计数。有什么方法可以在onDeleted
中执行此操作吗?(慢慢地,我似乎感觉到很多 Android 编码实际上是黑客行为 - 并且完成工作的代码大多使用无证或意想不到的途径。)
Ok, I figured out a way that doesn't involve having to store any persistent information. However even this looks like a hack to me. Inserted the following in the onReceive -
It works, and is the best solution I could find, but I am not very happy with it.
I needed to do this in
onReceive
and notonDeleted
, since theintent
object is required to dointent.getComponent()
. I don't like the fact because the documentation claims that one will rarely need to touch theonReceive
, and need to count remaining widgets seems to be a reasonable expectation inonDeleted
. However unfortunately it provides count of widgets being deleted, and not count remaining. Is there any way to do this in theonDeleted
instead?(Slowly I seem to get the feeling that a lot of Android coding is actually hacking - and the code to get a job done mostly uses undocumented or unexpected avenues.)
如果您为
Widget
创建了一个AppWidgetProvider
,它应该有一个onUpdate()
方法。onUpdate()
的参数之一是int[] ids
。这实际上是接收onUpdate()
请求的所有 AppWidget 实例的数组。如果ids
的大小为 1,那么下一个删除就是您的最后一个小部件。 :)希望这有帮助,
模糊逻辑
If you created a
AppWidgetProvider
for yourWidget
, it should have anonUpdate()
method. One of the arguments foronUpdate()
is theint[] ids
. This is actually an array of all of the AppWidget instances receivingonUpdate()
requests. If the size ofids
is 1, then the next delete is your last widget. :)Hope this helps,
FuzzicalLogic
我的应用程序也遇到了类似的问题,这就是我最终要做的事情,以确定我是否删除了最后一个小部件,这使我能够停止服务并更新已保存的首选项。
然后我只是测试了一下在
onStartCommand
上传递给我的服务的额外内容,如果“lastWidgetDeleted”定义为 true,那么我将停止服务并更新我的首选项。I had a similar issue with my application and this is what I ended up doing to figure out if I had deleted the last widget or not, which allowed me to stop my service and update a preference item that was saved.
Then I just tested looked at the extra passed to my service on
onStartCommand
and if has "lastWidgetDeleted" defined as true then I would stop the service and update my preferences.可能答案为时已晚,但仅当实例化第一个小部件以及为特定 appwidget 提供程序删除最后一个小部件时,才会调用 onEnabled 和 onDisabled 。因此,不需要上述 hacky 和其他技术。
如果您仍然有疑问,请浏览 Android文档
may be answer is too late but onEnabled and onDisabled is called only when first widget is instantiated and when last widget is removed for a particular appwidget provider. Hence the above hacky and other technecs are not required.
if still you have doubts please go through the Android doc