在應用程式內釘選「資訊一覽」小工具
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Android 8.0 (API 級別 26) 以上版本中,您可以讓使用者在應用程式內將小工具釘選到主畫面。直接在應用程式中宣傳小工具,是提高使用者參與度的絕佳方式,尤其是在使用者完成相關工作後,或是重複存取應用程式中的某項功能時。
建立 PIN 碼要求
如要啟動小工具固定作業,請使用 GlanceAppWidgetManager
類別的 requestPinGlanceAppWidget
方法。如果應用程式在較舊的 Android 版本上執行,這會傳回 false。不過,如果要求已成功傳送至系統,則會傳回 true。
以下範例說明如何建立 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 小工具。小工具成功釘選時,會觸發 PendingIntent
。successCallback
PendingIntent
處理 PIN 碼要求回應
使用者回應 PIN 碼要求對話方塊後,應用程式就會收到回應。如果使用者接受要求,系統就會將小工具釘選到主畫面,並觸發 successCallback
PendingIntent
。如果使用者拒絕要求,則不會有任何異動。
請注意,只有在小工具成功新增至主畫面時,才會觸發 successCallback
。如果使用者拒絕要求,或啟動器不支援釘選,就不會觸發這個事件。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-26 (世界標準時間)。
[null,null,["上次更新時間:2025-08-26 (世界標準時間)。"],[],[],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."]]