gpt4 book ai didi

java - onAppWidgetOptionsChanged() 在屏幕上唤醒调用。如何防止这种情况发生?

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:33 24 4
gpt4 key购买 nike

我有一个日历小部件,我每天中午 12:01 都会自行更新它,并且当大小发生变化时,我不需要自动更新。我还将 updatePeriodMillis 设置为 0。我遇到的问题是 App Widget 调用 onAppWidgetOptionsChanged():

  • 每次屏幕解锁
  • 每次带你回家

这对我来说不是问题,但问题是您可以瞬间看到应用程序小部件更新。有人知道有什么方法可以防止这种情况发生吗?

此处供引用的是我的 WidgetProvider 类的一部分

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (action != null) {
if (action.equals(ACTION_REFRESH) || action.equals(ACTION_NEXT) || action.equals(ACTION_PREVIOUS)) {
context.sendBroadcast(new Intent(action));
}
}

Bundle extra = intent.getExtras();
if (extra != null && extra.containsKey(KEY_APP_WIDGET_ID)) {
updateAppWidget(context, AppWidgetManager.getInstance(context), extra.getInt(KEY_APP_WIDGET_ID));
}
super.onReceive(context, intent);
}


@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
int width = Util.dp2px(newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH));
Intent broadcastIntent = new Intent(ACTION_REFRESH);
broadcastIntent.putExtra(KEY_SIZE_CHANGE, width);
context.sendBroadcast(broadcastIntent);
updateAppWidget(context, appWidgetManager, appWidgetId);
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}

private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

Intent intent = new Intent(context, WidgetService.class);
views.setRemoteAdapter(R.id.widget_calendar_container, intent);

// Buttons on widget click handlers
views.setOnClickPendingIntent(R.id.widget_refresh, getPendingSelfIntent(context, ACTION_REFRESH, appWidgetId));
views.setOnClickPendingIntent(R.id.widget_next, getPendingSelfIntent(context, ACTION_NEXT, appWidgetId));
views.setOnClickPendingIntent(R.id.widget_previous, getPendingSelfIntent(context, ACTION_PREVIOUS, appWidgetId));

// Widget Container click handler
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
homeIntent.setComponent(new ComponentName(context.getPackageName(), Home.class.getName()));
PendingIntent homePendingIntent = PendingIntent.getActivity(context, 0, homeIntent, 0);
views.setPendingIntentTemplate(R.id.widget_calendar_container, homePendingIntent);


// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_calendar_container);
}

最佳答案

我更喜欢另一个答案,但这就是我到目前为止得到的答案。

@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
int width = Util.dp2px(newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH));
if (width != WidgetRemoteViewsFactory.width) {
Intent broadcastIntent = new Intent(ACTION_REFRESH);
broadcastIntent.putExtra(KEY_SIZE_CHANGE, width);
LocalBroadcastManager.getInstance(context).sendBroadcast(broadcastIntent);
updateAppWidget(context, appWidgetManager, appWidgetId);
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}
}

我将我感兴趣的字段设置为 WidgetRemoteViewsFactory 上的静态字段,并且仅在该字段更改时进行广播。我似乎无法阻止 onAppWidgetOptionsChanged() 被调用,因为每次屏幕唤醒时都会广播和接收 AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED

Android 文档说

Called in response to the AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED broadcast when this widget has been layed out at a new size. - AppWidgetProvider#onAppWidgetOptionsChanged

但是每次屏幕唤醒时都会广播该操作。

如果大家有更好的解决方案,欢迎留言。必须有更好的解决方案,否则这是 Android 方面的错误。

关于java - onAppWidgetOptionsChanged() 在屏幕上唤醒调用。如何防止这种情况发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52141813/

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