ปักหมุดวิดเจ็ต Glance ในแอป
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ใน Android 8.0 (API ระดับ 26) ขึ้นไป คุณสามารถอนุญาตให้ผู้ใช้ปักหมุดวิดเจ็ตของคุณไว้ในหน้าจอหลักภายในแอปได้ การโปรโมตวิดเจ็ตภายในแอปโดยตรงเป็นวิธีที่ยอดเยี่ยมในการเพิ่มการมีส่วนร่วมของผู้ใช้ โดยเฉพาะหลังจากที่ผู้ใช้ทํางานที่เกี่ยวข้องเสร็จสมบูรณ์ หรือเมื่อผู้ใช้เข้าถึงฟีเจอร์ในแอปของคุณซ้ำๆ
สร้างคำขอ PIN
หากต้องการเริ่มการปักหมุดวิดเจ็ต ให้ใช้วิธี requestPinGlanceAppWidget
จากคลาส GlanceAppWidgetManager
สำหรับแอปที่ทำงานใน Android เวอร์ชันต่ำกว่า
ฟังก์ชันนี้จะแสดงผลเป็นเท็จ แต่หากส่งคำขอไปยังระบบสำเร็จ
ฟังก์ชันนี้จะแสดงค่าเป็นจริง
ตัวอย่างวิธีสร้างคำขอ PIN มีดังนี้
@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
เมื่อผู้ใช้ตอบกลับกล่องโต้ตอบคำขอ PIN แอปของคุณจะได้รับการตอบกลับ หากผู้ใช้ยอมรับคำขอ ระบบจะปักหมุดวิดเจ็ตของคุณไว้ที่หน้าจอหลักของผู้ใช้ และจะทริกเกอร์ successCallback
PendingIntent
หากผู้ใช้ปฏิเสธคำขอ ระบบจะไม่ดำเนินการใดๆ
โปรดทราบว่าระบบจะทริกเกอร์ successCallback
ก็ต่อเมื่อเพิ่มวิดเจ็ตลงในหน้าจอหลักสำเร็จแล้วเท่านั้น
ระบบจะไม่ทริกเกอร์หากผู้ใช้
ปฏิเสธคำขอหรือหาก Launcher ไม่รองรับการปักหมุด
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-26 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-26 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/5673ffc60b614daf028ee936227128eb8c4f9781/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."]]