위젯 검색 가능 여부

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

자바

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

위젯의 기능이 가장 관련성이 높은 경우 사용자가 위젯 선택 도구를 통해 또는 앱 내에서 위젯을 찾아 추가합니다. 자세한 내용은 검색 및 프로모션.