Widget bulunabilirliği

Android 8.0 (API düzeyi 26) ve sonraki sürümleri çalıştıran cihazlarda kullanıcılar sabitlenmiş kısayollar da oluşturur ana ekranlarına widget'ları sabitleyebileceğim. Sabitlenen kısayollara benzer şekilde, bu öğeler sabitlenmiş widget'lar kullanıcıların uygulamanızdaki belirli görevlere erişmesini sağlar ve aşağıdaki videoda gösterildiği gibi doğrudan uygulamadan ana ekrana eklenir.

Duyarlı düzen örneği
Şekil 2. Widget sabitleme örneği.

Kullanıcıların widget sabitlemesine izin verme

Uygulamanızda, aşağıdaki adımları uygulayarak sistemin desteklenen bir başlatıcıya widget sabitlemesi için istek oluşturabilirsiniz:

  1. Uygulamanızın manifest dosyasında bir widget beyan ettiğinizden emin olun.

  2. Aşağıdaki kod snippet'inde gösterildiği gibi requestPinAppWidget() yöntemini çağırın:

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

Kullanıcılar, widget'ın işlevi en alakalı olduğunda widget'ınızı widget seçici üzerinden veya uygulamanızdan keşfedip ekler. Daha fazla bilgi için Keşif ve tanıtım başlıklı makaleyi inceleyin.