屏幕唤醒时调用onAppWidgetOptionsChanged()。如何预防呢?

时间:2018-09-02 23:50:52

标签: java android

我有一个日历小部件,更改大小后,我每天也在12:01 AM自己更新它,因此我不需要自动更新。我还将updatePeriodMillis设置为0。我遇到的问题是App窗口小部件调用了onAppWidgetOptionsChanged()

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

对我来说这不是问题,但问题是您可以瞬间看到App Widget更新。有人知道我可以防止这种情况发生吗?

作为参考,这是我的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);
}

1 个答案:

答案 0 :(得分:0)

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

@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上将对width感兴趣的字段设为静态,并且仅在该字段更改时广播。我似乎无法阻止onAppWidgetOptionsChanged()的调用,因为每次屏幕唤醒时都会广播和接收AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED

Android文档说

  

以新尺寸布置此小部件时响应AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED广播而调用。 --   AppWidgetProvider#onAppWidgetOptionsChanged

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

如果有人有更好的解决方案,请随时发布。必须有更好的解决方案,否则这是Android方面的错误。