Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Pada perangkat yang menjalankan Android 8.0 (level API 26) dan yang lebih tinggi, peluncur yang memungkinkan pengguna membuat pintasan yang dipasangi pin juga memungkinkan mereka menyematkan widget ke layar utama. Seperti pintasan yang dipasangi pin, widget yang dipasangi pin ini memberi pengguna akses ke tugas tertentu di aplikasi Anda dan dapat ditambahkan ke layar utama langsung dari aplikasi, seperti yang ditunjukkan dalam video berikut.
Gambar 2. Contoh menyematkan widget.
Mengizinkan pengguna menyematkan widget
Di aplikasi, Anda dapat mengajukan permintaan pada sistem untuk menyematkan widget ke peluncur yang didukung dengan menyelesaikan langkah-langkah berikut:
Panggil metode
requestPinAppWidget(), seperti yang ditunjukkan dalam cuplikan kode berikut:
Kotlin
valappWidgetManager=AppWidgetManager.getInstance(context)valmyProvider=ComponentName(context,ExampleAppWidgetProvider::class.java)if(appWidgetManager.isRequestPinAppWidgetSupported()){// Create the PendingIntent object only if your app needs to be notified// when the user chooses to pin the widget. Note that if the pinning// operation fails, your app isn't notified. This callback receives the ID// of the newly pinned widget (EXTRA_APPWIDGET_ID).valsuccessCallback=PendingIntent.getBroadcast(/* context = */context,/* requestCode = */0,/* intent = */Intent(...),/* flags = */PendingIntent.FLAG_UPDATE_CURRENT)appWidgetManager.requestPinAppWidget(myProvider,null,successCallback)}
Java
AppWidgetManagerappWidgetManager=AppWidgetManager.getInstance(context);ComponentNamemyProvider=newComponentName(context,ExampleAppWidgetProvider.class);if(appWidgetManager.isRequestPinAppWidgetSupported()){// Create the PendingIntent object only if your app needs to be notified// when the user chooses to pin the widget. Note that if the pinning// operation fails, your app isn't notified. This callback receives the ID// of the newly pinned widget (EXTRA_APPWIDGET_ID).PendingIntentsuccessCallback=PendingIntent.getBroadcast(/* context = */context,/* requestCode = */0,/* intent = */newIntent(...),/* flags = */PendingIntent.FLAG_UPDATE_CURRENT);appWidgetManager.requestPinAppWidget(myProvider,null,successCallback);}
Panduan desain terkait
Pengguna menemukan dan menambahkan widget Anda melalui pemilih widget atau dari dalam aplikasi Anda saat fungsi widget paling relevan. Untuk mengetahui informasi selengkapnya, lihat
Penemuan dan promosi.
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-21 UTC.
[null,null,["Terakhir diperbarui pada 2025-08-21 UTC."],[],[],null,["# Widget discoverability\n\nOn devices running Android 8.0 (API level 26) and higher, launchers that let\nusers create [pinned shortcuts](/guide/topics/ui/shortcuts#shortcut-types) also\nlet them pin widgets onto their home screen. Similar to pinned shortcuts, these\n*pinned widgets* give users access to specific tasks in your app and can be\nadded to the home screen directly from the app, as shown in the following video.\n**Figure 2.**Example of pinning a widget.\n\nLet users pin a widget\n----------------------\n\nIn your app, you can create a request for the system to pin a widget onto a\nsupported launcher by completing the following steps:\n\n1. Make sure you [declare a widget in your app's manifest file](/guide/topics/appwidgets#Manifest).\n\n2. Call the\n [`requestPinAppWidget()`](/reference/android/appwidget/AppWidgetManager#requestPinAppWidget(android.content.ComponentName,%20android.os.Bundle,%20android.app.PendingIntent))\n method, as shown in the following code snippet:\n\n### Kotlin\n\n```kotlin\nval appWidgetManager = AppWidgetManager.getInstance(context)\nval myProvider = ComponentName(context, ExampleAppWidgetProvider::class.java)\n\nif (appWidgetManager.isRequestPinAppWidgetSupported()) {\n // Create the PendingIntent object only if your app needs to be notified\n // when the user chooses to pin the widget. Note that if the pinning\n // operation fails, your app isn't notified. This callback receives the ID\n // of the newly pinned widget (EXTRA_APPWIDGET_ID).\n val successCallback = PendingIntent.getBroadcast(\n /* context = */ context,\n /* requestCode = */ 0,\n /* intent = */ Intent(...),\n /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT)\n\n appWidgetManager.requestPinAppWidget(myProvider, null, successCallback)\n}\n```\n\n### Java\n\n```java\nAppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);\nComponentName myProvider = new ComponentName(context, ExampleAppWidgetProvider.class);\n\nif (appWidgetManager.isRequestPinAppWidgetSupported()) {\n // Create the PendingIntent object only if your app needs to be notified\n // when the user chooses to pin the widget. Note that if the pinning\n // operation fails, your app isn't notified. This callback receives the ID\n // of the newly pinned widget (EXTRA_APPWIDGET_ID).\n PendingIntent successCallback = PendingIntent.getBroadcast(\n /* context = */ context,\n /* requestCode = */ 0,\n /* intent = */ new Intent(...),\n /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT);\n\n appWidgetManager.requestPinAppWidget(myProvider, null, successCallback);\n}\n```\n| **Note:** If your app doesn't need to be notified of whether the system successfully pins a widget onto a supported launcher, you can pass in `null` as the third argument to `requestPinAppWidget()`.\n\nRelated design guidance\n-----------------------\n\nUsers discover and add your widget through the widget picker or from within your\napp when the widget's functionality is most relevant. For more information, see\n[Discovery and promotion](/design/ui/mobile/guides/widgets/discovery-promotion)."]]