12

可能重复:
小部件上的方向更改按钮没有响应后

我遇到了一个 appwidget 的问题,它在 xml 布局中有一个 ImageView,我为此注册了一个在 OnReceive 方法中处理的 pendingintent。一切正常,直到我改变手机方向。此时小部件不再起作用,我单击图像但没有任何反应。这个问题与这里的问题完全相同: 在小部件上的方向更改按钮没有响应之后

有什么问题,如何解决?谢谢。

4

4 回答 4

9

我最终设法重新创建了 OP 的无服务解决方案。这是记录的秘密:每次更新远程视图时,都必须更新您曾经更新的所有内容。我的应用程序正在更新一些视觉元素,但没有再次设置按钮处理程序。这导致处理程序停止工作 - 不是立即 - 只有在旋转改变之后,因此造成混乱。

如果这样做正确,您不需要拦截配置更改广播,您设置的最后一个远程视图将在轮换后再次使用。无需调用您的 AppWidgetProvider。

于 2011-02-24T14:52:32.337 回答
2

I had a similar issue, but my app is working well now with the solution suggested by Alex

"...solved without the help of a service, just setting again the setOnClickPendingIntent in the OnReceive method"

I tracked the onReceive method and it is called everytime I change the orientation of my device, so I called again the configuration of my widget on the onReceive method.

Anyway Im not sure this is the best solution to solve that problem, if anyone knows a better solution please share.

Thanks.

于 2012-01-12T22:50:12.650 回答
1

每当您更新小部件的外观时(使用 Activity 或您的广播接收器 [应用小部件提供程序]),您还必须为点击处理程序重新分配所有 PendingIntent,然后updateAppWidget()正常调用。

示例setTextViewText()

// This will update the Widget, but cause it to
// stop working after an orientation change.
updateWidget()
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.widget_text_view, "Updated widget");

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}


// This is the correct way to update the Widget,
// so that it works after orientation change.
updateWidget()
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.widget_text_view, "Updated widget");

Intent intent = new Intent(context, MyWidgetActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, ...);
remoteViews.setOnClickPendingIntent(R.id.widget_click_button, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
于 2012-03-13T17:22:34.183 回答
0

请参阅此处以获取可能的解释,或进一步尝试:

http://permalink.gmane.org/gmane.comp.handhelds.android.devel/98008

或者

http://groups.google.com/group/android-developers/browse_thread/thread/578a4429c369c27c/273a53a96ddd10c5?lnk=gst&q=Widget+does+not+respond+when+phone+orientation+changes#273a53a96ddd10c5

但目前尚不清楚真正的解决方案是什么。

于 2011-12-27T17:24:06.363 回答