Trên các thiết bị chạy Android 8.0 (API cấp 26) trở lên, trình chạy cho phép người dùng tạo lối tắt được ghim cũng cho phép họ ghim tiện ích vào màn hình chính. Tương tự như lối tắt được ghim, các lối tắt này tiện ích được ghim cho phép người dùng truy cập vào các nhiệm vụ cụ thể trong ứng dụng của bạn và có thể thêm vào màn hình chính ngay trong ứng dụng, như trong video dưới đây.

Cho phép người dùng ghim tiện ích
Trong ứng dụng của mình, bạn có thể tạo yêu cầu để hệ thống ghim tiện ích vào một hỗ trợ trình chạy bằng cách hoàn thành các bước sau:
Gọi phương thức
requestPinAppWidget()
như trong đoạn mã sau:
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); }
Hướng dẫn thiết kế có liên quan
Người dùng khám phá và thêm tiện ích của bạn thông qua bộ chọn tiện ích hoặc trong khi chức năng của tiện ích là phù hợp nhất. Để biết thêm thông tin, hãy xem phần Khám phá và quảng bá.