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

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

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