在搭載 Android 8.0 (API 級別 26) 以上版本的裝置上,可讓使用者建立固定捷徑的啟動器也讓他們將小工具固定在主畫面。與固定捷徑類似,這些固定的小工具可讓使用者存取應用程式中的特定工作,而且可以直接從應用程式新增至主畫面,如以下影片所示。
允許使用者固定小工具
您可以在應用程式中建立要求,讓系統將小工具固定到支援的啟動器,步驟如下:
呼叫
requestPinAppWidget()
方法,如以下程式碼片段所示:
Kotlin
val appWidgetManager = AppWidgetManager.getInstance(context) val myProvider = 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). val successCallback = PendingIntent.getBroadcast( /* context = */ context, /* requestCode = */ 0, /* intent = */ Intent(...), /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT) appWidgetManager.requestPinAppWidget(myProvider, null, successCallback) }
Java
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName myProvider = new ComponentName(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). PendingIntent successCallback = PendingIntent.getBroadcast( /* context = */ context, /* requestCode = */ 0, /* intent = */ new Intent(...), /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT); appWidgetManager.requestPinAppWidget(myProvider, null, successCallback); }
相關設計指南
當使用者透過小工具挑選器或在應用程式中找到小工具時,如果小工具功能最相關,使用者就能從應用程式中找到並新增小工具。詳情請參閱「探索與宣傳」。