RemoteViews 上的 partiallyUpdateAppWidget 不起作用
partiallyUpdateAppWidget on RemoteViews not working
在 AppWidget
的 RemoteViews
中,我有一个 AdapterViewFlipper
,当用户单击 AppWidget
中的按钮时,它应该翻转。根据官方文档,这应该通过调用 showNext on the RemoteViews
. The AppWdiget
should then be updated with partiallyUpdateAppWidget 来完成,文档中甚至有 showNext()
函数作为示例。我已经尝试过实现这个,但我很可能犯了一些我似乎无法弄清楚的愚蠢错误。
public class WidgetProvider extends AppWidgetProvider {
public static final String NEXT = "next";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent nextIntent = new Intent(context, WidgetProvider.class);
nextIntent.setAction(NEXT);
views.setOnClickPendingIntent(R.id.next_button,
PendingIntent.getBroadcast(context, appWidgetId, nextIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
views.setRemoteAdapter(R.id.view_flipper, new Intent(context,
WidgetRemoteViewService.class));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(NEXT)) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
remoteViews.showNext(R.id.view_flipper);
AppWidgetManager.getInstance(context).partiallyUpdateAppWidget(
AppWidgetManager.getInstance(context).getAppWidgetIds(
new ComponentName(context, WidgetProvider.class)),
remoteViews);
}
super.onReceive(context, intent);
}
}
当用 updateAppWidget
替换 partiallyUpdateAppWidget
时,下一个按钮一直有效,直到方向改变或 phone 进入睡眠状态,这会强制重绘上次存储的 RemoteViews
.这让我相信我犯的错误与部分更新有关,但我无法弄清楚我做错了什么。
你在哪个 API 级别测试?
也许 withpartiallyUpdateAppWidget
的问题与这个未解决的问题有关 issuetracker.google.com/issues/37136552
在 AppWidget
的 RemoteViews
中,我有一个 AdapterViewFlipper
,当用户单击 AppWidget
中的按钮时,它应该翻转。根据官方文档,这应该通过调用 showNext on the RemoteViews
. The AppWdiget
should then be updated with partiallyUpdateAppWidget 来完成,文档中甚至有 showNext()
函数作为示例。我已经尝试过实现这个,但我很可能犯了一些我似乎无法弄清楚的愚蠢错误。
public class WidgetProvider extends AppWidgetProvider {
public static final String NEXT = "next";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent nextIntent = new Intent(context, WidgetProvider.class);
nextIntent.setAction(NEXT);
views.setOnClickPendingIntent(R.id.next_button,
PendingIntent.getBroadcast(context, appWidgetId, nextIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
views.setRemoteAdapter(R.id.view_flipper, new Intent(context,
WidgetRemoteViewService.class));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(NEXT)) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
remoteViews.showNext(R.id.view_flipper);
AppWidgetManager.getInstance(context).partiallyUpdateAppWidget(
AppWidgetManager.getInstance(context).getAppWidgetIds(
new ComponentName(context, WidgetProvider.class)),
remoteViews);
}
super.onReceive(context, intent);
}
}
当用 updateAppWidget
替换 partiallyUpdateAppWidget
时,下一个按钮一直有效,直到方向改变或 phone 进入睡眠状态,这会强制重绘上次存储的 RemoteViews
.这让我相信我犯的错误与部分更新有关,但我无法弄清楚我做错了什么。
你在哪个 API 级别测试?
也许 withpartiallyUpdateAppWidget
的问题与这个未解决的问题有关 issuetracker.google.com/issues/37136552