在应用内固定 Glance widget
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在 Android 8.0(API 级别 26)及更高版本中,您可以让用户在应用内将您的 widget 固定到其主屏幕。直接在应用内推广 widget 是提高用户互动度的绝佳方式,尤其是在用户完成相关任务后或用户反复访问应用中的某项功能时。
创建 PIN 码请求
如需启动 widget 固定,请使用 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
是接收 widget 回调的类,MyWidget
是要固定的 Glance widget。successCallback
是一个 PendingIntent
,当 widget 成功固定时触发。
处理 PIN 码请求响应
当用户响应 PIN 码请求对话框时,您的应用会收到响应。如果用户接受该请求,您的 widget 将固定到其主屏幕,并触发 successCallback
PendingIntent
。如果用户拒绝该请求,则不会发生任何情况。
请务必注意,只有在成功将 widget 添加到主屏幕时,系统才会触发 successCallback
。如果用户拒绝该请求或启动器不支持固定,则不会触发此回调。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-26。
[null,null,["最后更新时间 (UTC):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."]]