创建片段时,我将执行以下代码。但是,小部件一旦更新,便无法再次更新。
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext());
RemoteViews remoteViews = new RemoteViews(getContext().getPackageName(), R.layout.recipe_appwidget);
ComponentName thisWidget = new ComponentName(getContext(), RecipeAppWidgetProvider.class);
Intent intent = new Intent(getContext(), RecipeWidgetService.class);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(R.id.listViewWidget, intent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.layout.recipe_appwidget);