可能是重复问题:
更改方向后,小部件上的按钮无响应
我遇到了一个问题,我的appwidget中有一个ImageView,我在xml布局中注册了一个pendingintent,该pendingintent在OnReceive方法中处理。 一切都正常工作,直到我改变手机方向。此时,小部件不再起作用,我点击图像,但什么也不会发生。 这个问题与这个问题完全相同: 更改方向后,小部件上的按钮无响应 问题出在哪里,如何解决? 谢谢。
可能是重复问题:
更改方向后,小部件上的按钮无响应
我遇到了一个问题,我的appwidget中有一个ImageView,我在xml布局中注册了一个pendingintent,该pendingintent在OnReceive方法中处理。 一切都正常工作,直到我改变手机方向。此时,小部件不再起作用,我点击图像,但什么也不会发生。 这个问题与这个问题完全相同: 更改方向后,小部件上的按钮无响应 问题出在哪里,如何解决? 谢谢。
我遇到了类似的问题,但是按照Alex建议的解决方案,我的应用现在运行良好。
"......在OnReceive方法中重新设置setOnClickPendingIntent,就可以不借助服务而解决问题"
我追踪了onReceive方法,发现每次我改变设备的方向时都会调用它,因此我在onReceive方法中再次调用了小部件的配置。
总之,我不确定这是否是解决该问题的最佳方法,如果有人知道更好的解决方案,请分享。
谢谢。
每当您更新小部件的外观时(使用Activity或Broadcast Receiver [App widget provider]),您还必须重新分配所有单击处理程序的PendingIntents,然后像往常一样调用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);
}
请参考此处以获取可能的解释或进一步尝试的内容:
http://permalink.gmane.org/gmane.comp.handhelds.android.devel/98008
或者
但目前尚不清楚真正的解决方案是什么。