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

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

ユーザーはウィジェット選択ツールを使用するか、 ウィジェットの機能が適切なときにのみアプリに表示されます。詳細については、見つけやすくする、宣伝するをご覧ください。