ウィジェットの見つけやすさ

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

レスポンシブ レイアウトの例
図 2. ウィジェットを固定する例。

ユーザーがウィジェットを固定できるようにする

以下の手順を実行することで、アプリ内で、サポートされているランチャーにウィジェットを固定するリクエストを作成できます。

  1. アプリのマニフェスト ファイルでウィジェットを宣言してください。

  2. 次のコード スニペットに示すように、requestPinAppWidget() メソッドを呼び出します。

Kotlin

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) }

Java

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); }

ユーザーは、ウィジェット選択ツールから、またはウィジェットの機能が最も関連性が高いときにアプリ内からウィジェットを見つけて追加します。詳細については、見つけやすくする、宣伝するをご覧ください。