重新创建小部件时,Android 主屏幕小部件的 onReceive 不起作用

发布于 2024-09-05 06:36:57 字数 151 浏览 5 评论 0原文

每当我在手机上重新创建主屏幕小部件时,都不会调用 onReceive() 方法。

问题是它不会响应我指定其功能驻留在 onReceive 方法中的按钮按下。

问题并不驻留在模拟器上,但当我用手机测试它时,它没有响应。

最好的解决方案是什么?

whenever I recreate the home screen widget on my phone, the onReceive() method is not called.

the problem would be that it doesn't respond to Button press that I assign which its function resides in the onReceive method..

The issue does not reside on the emulator but when I tested it with my phone, it doesnt respond to it.

What would be the best solution for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不疑不惑不回忆 2024-09-12 06:36:57

因为您没有任何更详细的内容(可能是一些关于如何注册接收器以及如何绑定待处理意图的源代码)。您的手机可能是带有滑动键盘的型号,或者那些主屏幕可以改变方向。因为当主屏幕的屏幕方向发生变化(或任何硬件配置)时,主屏幕会膨胀并重新创建。因此,对于您的按钮来说,最初绑定到它的意图在重新创建后就消失了。根据文档(抱歉,找不到链接),充气机只会从远程视图获取最新更新。因此,以下内容将无法工作:

RemoteViews rv = ...;
// Assign the button to some pending intent
rv.setOnClickPendingIntent(View, pi);
AppWidgetManager.Update(...);

// And after sometime, you make changes to the rv
rv.setFloat();
// And update again
AppWidgetManager.Update(...);

仍然可以,按钮将按预期触发待处理意图,但是,如果由于任何原因配置发生更改并且主屏幕无效(并重新创建),则appwidgetmanager 将仅根据最后更新进行更新,该更新未指定有关点击意图的任何信息。

解决方案是,每次更新远程视图时,都必须设置所有待处理的意图。我不确定它会如何影响性能,但这是我可以拥有的唯一工作方法。 (但我的小部件每秒更新 16 次,至少它可以工作:)

Since you don't have any more detail stuff (maybe some source code on how you register the receiver and how you bind the pending intent). Probably your phone is a model with sliding keypad, or those home screen can change orientation. Because when screen orientation of home screen changed (or any hardware configuration), the home screen is inflated and recreated. So, for your buttons, the intent that originally bound to it is gone after the recreation. According to the documentation (sorry, can't find the link), the inflater will only get the latest update from remoteviews. So, the following would not work:

RemoteViews rv = ...;
// Assign the button to some pending intent
rv.setOnClickPendingIntent(View, pi);
AppWidgetManager.Update(...);

// And after sometime, you make changes to the rv
rv.setFloat();
// And update again
AppWidgetManager.Update(...);

It is still ok and the button will fire the Pending Intent as expected, but, if for any reason configuration changed and the home screen is invalidated (and recreated), the appwidgetmanager will only update according to the last update, which, did not specify anything about the clicking intent.

Solution to this is, everytime you update the remote view, you have to set all the pending intent as well. I am not sure how it would impacts the performance, but it is the only working method I can have. (but my widget is updating like 16 times/second and at least it works :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文