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, sistemin bir widget'ı belirli bir ekrana sabitlemesi için aşağıdaki adımları tamamlayarak desteklenen başlatıcıyı kullanabilirsiniz:

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

  2. Şunu çağırın: 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ızı widget seçici aracılığıyla veya en alakalı uygulamalar arasında yer alır. Daha fazla bilgi için bkz. Keşif ve tanıtım.