从活动更新的解决方案不起作用。 此代码在我的应用中无效:
Intent intentq = new Intent(getActivity(), Widget.class);
intentq.setAction("android.appwidget.action.APPWIDGET_UPDATE");
int ids[] = AppWidgetManager.getInstance(getActivity().getApplication()).getAppWidgetIds(new ComponentName(getActivity().getApplication(), Widget.class));
intentq.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
getActivity().sendBroadcast(intentq);
答案 0 :(得分:0)
我在我的片段中使用此方法,以便通过单击updateButton来更新小部件。我希望这种方法可以帮助您解决问题。
private void updateWidgetScreen(String updateData) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(parentActivity);
RemoteViews remoteViews = new RemoteViews(parentActivity.getPackageName(), R.layout.widget);
ComponentName thisWidget = new ComponentName(parentActivity, WidgetProvider.class);
remoteViews.setTextViewText(R.id.remaining_money, updateData);
Intent intent = new Intent(parentActivity, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, MainUtils.getWidgetID(prefs));
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(MainUtils.getWidgetID(prefs), R.id.list_view, intent);
Intent clickIntent = new Intent(parentActivity, MainActivity.class);
PendingIntent clickPI = PendingIntent.getActivity(parentActivity, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.list_view, clickPI);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
Log.e("updateWidgetScreen", updateData+" updateData - widgetid: "+ MainUtils.getWidgetID(prefs));
}