Android 8.0(API レベル 26)以降を実行しているデバイスでは、固定ショートカットを作成できるランチャーで、ウィジェットをホーム画面に固定することもできます。固定ショートカットと同様に、そのようにして固定したウィジェットから、ユーザーはアプリ内の特定のタスクにアクセスできます。また、次の動画に示すように、アプリから直接ホーム画面に追加することもできます。

ユーザーにウィジェットの固定を許可する
アプリでは、システムに対するリクエストを作成して、ウィジェットを 統合するには、次の手順を行います。
アプリのマニフェスト ファイルでウィジェットを宣言してください。
次のコード スニペットに示すように、
requestPinAppWidget()
メソッドを呼び出します。
val appWidgetManager = AppWidgetManager.getInstance(context) val myProvider = ComponentName(context, ExampleAppWidgetProvider::class.java) if (appWidgetManager.isRequestPinAppWidgetSupported()) { // Create the PendingIntent object only if your app needs to be notified // when the user chooses to pin the widget. Note that if the pinning // operation fails, your app isn't notified. This callback receives the ID // of the newly pinned widget (EXTRA_APPWIDGET_ID). val successCallback = PendingIntent.getBroadcast( /* context = */ context, /* requestCode = */ 0, /* intent = */ Intent(...), /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT) appWidgetManager.requestPinAppWidget(myProvider, null, successCallback) }
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName myProvider = new ComponentName(context, ExampleAppWidgetProvider.class); if (appWidgetManager.isRequestPinAppWidgetSupported()) { // Create the PendingIntent object only if your app needs to be notified // when the user chooses to pin the widget. Note that if the pinning // operation fails, your app isn't notified. This callback receives the ID // of the newly pinned widget (EXTRA_APPWIDGET_ID). PendingIntent successCallback = PendingIntent.getBroadcast( /* context = */ context, /* requestCode = */ 0, /* intent = */ new Intent(...), /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT); appWidgetManager.requestPinAppWidget(myProvider, null, successCallback); }
関連する設計ガイダンス
ユーザーはウィジェット選択ツールを使用するか、 ウィジェットの機能が適切なときにのみアプリに表示されます。詳細については、次をご覧ください: 発見とプロモーション。