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);}
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],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)."]]