小部件上的方向更改button没有响应

我有一个小部件上的两个button,用于更改小部件中的某些项目,如果在手机上改变方向,则button什么也不做。 我阅读http://developer.android.com/guide/topics/resources/runtime-changes.html但这是关于活动而不是小部件。

@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); Intent active = new Intent(context, TvWidget.class); active.setAction(ACTION_WIDGET_RECEIVER); mDbHelper = new DbAdapter(context); fillChannelList(context, appWidgetIds[appWidgetIds.length-1]); Set<Integer> keys = channelsImages.keySet(); Iterator<Integer> iter = keys.iterator(); while(iter.hasNext()) { if(channelId == 0) { channelId = iter.next(); break; } } SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); Editor edit = settings.edit(); edit.putInt("channelId", channelId); edit.putInt("appWidgetIds", appWidgetIds[appWidgetIds.length-1]); edit.commit(); active.putExtra("net.aerosoftware.tvvodic.appWidgetIds", appWidgetIds); PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); remoteViews.setOnClickPendingIntent(R.id.button_next, actionPendingIntent); Intent refresh = new Intent(context, TvWidget.class); refresh.setAction(ACTION_WIDGET_REFRESH); refresh.putExtra("net.aerosoftware.tvvodic.appWidgetIds", appWidgetIds); PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0, refresh, 0); remoteViews.setOnClickPendingIntent(R.id.button_refresh, refreshPendingIntent); updateView(context); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); super.onUpdate(context, appWidgetManager, appWidgetIds); } 

Solutions Collecting From Web of "小部件上的方向更改button没有响应"

我会build议创build一个服务(可能在你的AppWidgetProvider子类),并覆盖onConfigurationChanged()方法。 使用这个服务将允许你委托你的业务逻辑由服务来处理,构build和更新你的小部件。 它也可以让你pipe理轮换。 如果你正在执行任何阻塞操作,那么这个服务将是一个产生一个线程的好地方,并将结果返回给主UI线程以避免ANR。

我会build议像下面这样的东西:

 public class MyWidget extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { context.startService(new Intent(context, MyUpdateService.class)); } public static class MyUpdateService extends Service { @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); // Update the widget RemoteView remoteView = buildRemoteView(this); // Push update to homescreen pushUpdate(remoteView); // No more updates so stop the service and free resources stopSelf(); } public RemoteViews buildRemoteView(Context context) { RemoteView updateView = null; updateView = new RemoteViews(context.getPackageName(), R.layout.my_widget_layout); // Your code to build and update the remote view return updateView; } @Override public void onConfigurationChanged(Configuration newConfig) { int oldOrientation = this.getResources().getConfiguration().orientation; if(newConfig.orientation != oldOrientation) { // Update the widget RemoteView remoteView = buildRemoteView(this); // Push update to homescreen pushUpdate(remoteView); } } private void pushUpdate(RemoteView remoteView) { ComponentName myWidget = new ComponentName(this, MyWidget.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(myWidget, updateViews); } } } 

也看看这个链接: http : //android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html

此外,请务必表明您有兴趣在清单中接收轮换更改通知。 像这样的东西可以工作:android:configChanges =“keyboardHidden | orientation”在清单内的服务声明中声明。

希望有所帮助!

每当你更新widget的外观时(使用Activity或者Broadcast Receiver [App Widget Provider]),你也必须重新分配点击处理器的所有PendingIntents,然后updateAppWidget()调用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); } 

这个问题可能是由于Widget上有两个button,这会导致问题。 看这里:

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

但目前还不清楚真正的解决scheme是什么(一个不需要创build后台服务的解决scheme)。

如果你使用

 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 

要更改远程视图对象中特定视图的某些状态,还需要更改其他视图,因为方向更改事件将只记住您用来更新小部件的最后一个RemoteViews对象。 它将使用最后使用的RemoteViews对象重新创build窗口小部件。 这就是为什么您总是需要更新RemoteViews对象中的所有视图以及视图的所有侦听器。

其余部分将由操作系统自动处理。