且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过代码重新添加时,小部件不响应

更新时间:2023-12-05 21:32:28

我们可以调用一个对话框,要求用户允许我们的应用程序使用以下代码绑定小部件ID:

We can invoke a dialog asking user to permit our app to be able to bind widget IDs with the following code:

Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, sSavedWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, mWidgetInfo.provider);
startActivityForResult(intent, REQUEST_BIND_WIDGET);

并绑定窗口小部件ID,以使其实际可用于:

And bind widget IDs to make them actually work with:

Bundle opts = new Bundle();
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, maxWidth);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);

widgetManager.bindAppWidgetIdIfAllowed(widgetId, mWidgetInfo, opts);

有关更多信息,请参见:

For more information refer:

  • AppWidgetManager.ACTION_APPWIDGET_BIND
  • AppWidgetManager.bindAppWidgetIdIfAllowed()

我希望有人能在5年前回答同样的问题.

I wish someone would have answered the same 5 years ago.