יכולת הגילוי של ווידג'טים

במכשירים עם 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);
}

המשתמשים מגלים את הווידג'ט ומוסיפים אותו דרך הכלי לבחירת ווידג'טים או מתוך האפליקציה, כשהפונקציונליות של הווידג'ט רלוונטית ביותר. מידע נוסף זמין במאמר הבא: גילוי וקידום.