הצמדת ווידג'טים של Glance באפליקציה
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
ב-Android 8.0 (רמת API 26) ואילך, אתם יכולים לאפשר למשתמשים להצמיד את הווידג'טים שלכם למסך הבית מתוך האפליקציה. קידום הווידג'טים ישירות באפליקציה הוא דרך מצוינת להגביר את מעורבות המשתמשים, במיוחד אחרי שהמשתמשים משלימים משימה קשורה, או כשהמשתמשים ניגשים שוב ושוב לתכונה באפליקציה.
יצירת בקשת קוד אימות
כדי להצמיד ווידג'ט, משתמשים ב-method requestPinGlanceAppWidget
מהקלאס GlanceAppWidgetManager
. אם האפליקציות פועלות בגרסאות נמוכות יותר של Android, הפונקציה מחזירה את הערך false. אבל אם הבקשה נשלחת למערכת בהצלחה, הפונקציה מחזירה true.
הנה דוגמה לאופן שבו אפשר ליצור בקשה לקוד אימות:
@Composable
fun AnInAppComposable() {
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
Button(
onClick = {
coroutineScope.launch {
GlanceAppWidgetManager(context).requestPinGlanceAppWidget(
receiver = MyWidgetReceiver::class.java,
preview = MyWidget(),
previewState = DpSize(245.dp, 115.dp)
)
}
}
) {}
}
בדוגמה הזו, MyWidgetReceiver
הוא הסיווג שמקבל את הקריאות החוזרות של הווידג'ט, ו-MyWidget
הוא הווידג'ט Glance שרוצים להצמיד. successCallback
הוא PendingIntent
שמופעל כשהווידג'ט מוצמד בהצלחה.
טיפול בתשובה לבקשת קוד אימות
כשמשתמש מגיב לתיבת הדו-שיח של בקשת ה-PIN, האפליקציה מקבלת תגובה. אם המשתמש יאשר את הבקשה, הווידג'ט שלכם יוצמד למסך הבית שלו, והאירוע successCallback
PendingIntent
יופעל. אם המשתמש דוחה את הבקשה, לא קורה כלום.
חשוב לציין שהפעולה successCallback
מופעלת רק אם הווידג'ט נוסף בהצלחה למסך הבית. היא לא מופעלת אם המשתמש דוחה את הבקשה או אם מרכז האפליקציות לא תומך בהצמדה.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-08-27 (שעון UTC).
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-08-27 (שעון UTC)."],[],[],null,["With Android 8.0 (API level 26) and higher, you can let users pin your\nwidgets to their home screen within your app. Promoting widgets directly within\nyour app is a great way to increase user engagement, especially after a\nuser completes a related task, or when a user repeatedly accesses a feature in\nyour app.\n\nCreate a Pin Request\n\nTo initiate widget pinning, use the [`requestPinGlanceAppWidget`](/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager#requestPinGlanceAppWidget(java.lang.Class,androidx.glance.appwidget.GlanceAppWidget,kotlin.Any,android.app.PendingIntent)) method\nfrom the [`GlanceAppWidgetManager`](/reference/kotlin/androidx/glance/appwidget/GlanceAppWidgetManager) class. For apps running on lower versions\nof Android, this returns false. However if the request is successfully sent\nto the system, this returns true.\n\nHere is an example of how you can create a pin request:\n\n\n```kotlin\n@Composable\nfun AnInAppComposable() {\n val context = LocalContext.current\n val coroutineScope = rememberCoroutineScope()\n Button(\n onClick = {\n coroutineScope.launch {\n GlanceAppWidgetManager(context).requestPinGlanceAppWidget(\n receiver = MyWidgetReceiver::class.java,\n preview = MyWidget(),\n previewState = DpSize(245.dp, 115.dp)\n )\n }\n }\n ) {}\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/glance/GlancePinAppWidget.kt#L44-L59\n```\n\n\u003cbr /\u003e\n\nIn this example, `MyWidgetReceiver` is the class that receives the widget's\ncallbacks, and `MyWidget` is the Glance widget you want to pin. The\n`successCallback` is a `PendingIntent` that is triggered when the widget is\nsuccessfully pinned.\n\nHandle the Pin Request Response\n\nWhen a user responds to the pin request dialog, your app receives a\nresponse. If the user accepts the request, your widget is pinned to their\nhome screen, and the `successCallback` `PendingIntent` is triggered. If the\nuser denies the request, nothing happens.\n\nIt is important to note that the `successCallback` is only triggered if the\nwidget is successfully added to the home screen. It is not triggered if the user\ndenies the request or if the launcher does not support pinning."]]