gpt4 book ai didi

java - 小部件不会更新/重画

转载 作者:行者123 更新时间:2023-12-01 22:24:39 27 4
gpt4 key购买 nike

我有一个从 SQLite 加载图像的小部件。每当我添加或删除项目时它都会更新,但图像不会更新。我已经尝试向 AppWidgetProvider 类发送广播,但它也不起作用。

class that extend AppWidgetProvider

private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Intent intent = new Intent(context, StackWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.fav_movie_widget);
views.setRemoteAdapter(R.id.stack_view, intent);
views.setEmptyView(R.id.stack_view, R.id.empty_view);

Intent toastIntent = new Intent(context, FavMovieWidget.class);
toastIntent.setAction(FavMovieWidget.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);

appWidgetManager.updateAppWidget(appWidgetId, views);
Log.e(TAG, "onUpdate2: start" );
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.e(TAG, "onUpdate: start" );
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}

activity for update

Context context = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, FavMovieWidget.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stack_view);

what i already try

Intent intent = new Intent(this, FavMovieWidget.class);       
intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(getApplication(), FavMovieWidget.class));
intent.putExtra(appWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

the widget image not update preview widget

最佳答案

通过将其添加到 updateAppWidget 来完成。这将在我的服务上触发 onGetViewFactory。

Random random = new Random();
intent.setType(String.valueOf(random.nextInt(1000)));

并在我的 Activity 中使用 sendbroadcast

Intent intent = new Intent(this, FavMovieWidget.class);       
intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(getApplication(), FavMovieWidget.class));
intent.putExtra(appWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

关于java - 小部件不会更新/重画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58570661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com