删除所有实例并添加另一个实例后,应用程序小部件停止响应输入
当我将小部件的一个或多个实例添加到主屏幕,将它们全部删除,然后添加另一个时,我遇到了麻烦。
这是我用来为每个按钮设置“onClick”意图的代码(按钮是一种资源)
protected void matchButtonToAction(Context context, RemoteViews views, String action, int button) {
Intent intent = new Intent(context, MightyToggleWidget.class);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(button, pendingIntent);
}
只有当我删除所有实例并添加一个实例时才会发生这种情况。如果我添加 2,删除 1,然后添加 1,它仍然有效。
它仍在接收电池事件并对其做出反应,但不接收其他事件(例如 wifi 状态更改事件)
任何想法可能会导致此问题? 谢谢!
I am having trouble when I add a one or more instances of my widget to the home screen, remove them all, then add another.
Here is the code I use to set the "onClick" intent for each button (button is a resource)
protected void matchButtonToAction(Context context, RemoteViews views, String action, int button) {
Intent intent = new Intent(context, MightyToggleWidget.class);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(button, pendingIntent);
}
This only happens when I remove ALL instances and add one. If I add 2, remove 1 and add 1 it still works.
It is still receiving and reacting to battery events, but not other events (such as wifi state change events)
Any ideas what could be causing this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经找到问题了。当添加第一个小部件时, onEnabled 事件会在开始时触发。当最后一个小部件被删除时, onDisabled 事件会被触发,但当您尝试添加另一个“第一个”小部件时, onEnabled 事件不会被触发。
我的代码与这些事件中的示例类似。这导致 packagemanager 取消注册我的 AppWidgetProvider 来接收这些事件。
我添加了一个静态方法,当配置活动添加一个调用 PackageManager 的新实例时,会调用该方法。这是代码,如果有人觉得有用的话
希望这会有所帮助!
I have found the problem. The onEnabled event gets triggered at the start when the first widget is added. The onDisabled event gets triggered when the last widget is removed, but the onEnabled event does not get triggered when you try to add another "first" widget.
I had code that was similar to the examples in those events. This resulted in the packagemanager unregistering my AppWidgetProvider from receiving these events
I have added a static method that is called when the Configuration Activity adds a new instance that calls the PackageManager. Here is the code if anyone finds it useful
Hope this helps!