Menyematkan widget Sekilas dalam aplikasi
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Dengan Android 8.0 (level API 26) dan yang lebih tinggi, Anda dapat mengizinkan pengguna menyematkan widget ke layar utama mereka dalam aplikasi Anda. Mempromosikan widget secara langsung dalam aplikasi Anda adalah cara yang bagus untuk meningkatkan interaksi pengguna, terutama setelah pengguna menyelesaikan tugas terkait, atau saat pengguna berulang kali mengakses fitur dalam aplikasi Anda.
Membuat Permintaan Pin
Untuk memulai penyematan widget, gunakan metode requestPinGlanceAppWidget
dari class GlanceAppWidgetManager
. Untuk aplikasi yang berjalan di versi Android yang lebih rendah, fungsi ini akan menampilkan nilai salah (false). Namun, jika permintaan berhasil dikirim ke sistem, nilai benar (true) akan ditampilkan.
Berikut adalah contoh cara membuat permintaan 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)
)
}
}
) {}
}
Dalam contoh ini, MyWidgetReceiver
adalah class yang menerima callback
widget, dan MyWidget
adalah widget Glance yang ingin Anda sematkan. successCallback
adalah PendingIntent
yang dipicu saat widget berhasil disematkan.
Menangani Respons Permintaan Pin
Saat pengguna merespons dialog permintaan pin, aplikasi Anda akan menerima respons. Jika pengguna menyetujui permintaan, widget Anda akan disematkan ke layar utama mereka, dan successCallback
PendingIntent
akan dipicu. Jika pengguna menolak permintaan, tidak ada yang terjadi.
Penting untuk diperhatikan bahwa successCallback
hanya dipicu jika
widget berhasil ditambahkan ke layar utama. Pintasan tidak dipicu jika pengguna menolak permintaan atau jika peluncur tidak mendukung penyematan.
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-08-26 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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."]]