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

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

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