感谢您关注这个问题。我将更新 Android 文档以反映这一点。
但是,我们建议不要在 onUpdate() 中强制调用 onAppWidgetOptionsChanged(),而是推荐如下方法:
public class AppWidget extends AppWidgetProvider {
....
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
updateWidget(context, appWidgetManager, appWidgetId, options);
}
}
public void onAppWidgetOptionsChanged (Context context, AppWidgetManager
appWidgetManager, int appWidgetId, Bundle newOptions) {
updateWidget(context, appWidgetManager, appWidgetId, newOptions);
}
private void updateWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, Bundle options) {
// Do whatever you need to do to the widget. Include any customization based
// on the size, included in the options. Be sure to have a graceful fallback if
// no valid size data is included in the bundle.
}
...
}