การค้นพบวิดเจ็ตได้

สำหรับอุปกรณ์ที่ใช้ Android 8.0 (API ระดับ 26) ขึ้นไป Launcher ที่ช่วยให้ ผู้ใช้สร้างทางลัดที่ปักหมุดไว้ด้วย ให้ตรึงวิดเจ็ตไว้บนหน้าจอหลักได้ ทางลัดเหล่านี้คล้ายกับทางลัดที่ปักหมุดไว้ วิดเจ็ตที่ปักหมุดไว้ช่วยให้ผู้ใช้เข้าถึงงานที่เจาะจงในแอปของคุณได้ ลงในหน้าจอหลักโดยตรงจากแอปพลิเคชัน ดังที่แสดงในวิดีโอต่อไปนี้

วันที่ ตัวอย่างเลย์เอาต์ที่ปรับเปลี่ยนตามอุปกรณ์
รูปที่ 2 ตัวอย่างการปักหมุดวิดเจ็ต

อนุญาตให้ผู้ใช้ปักหมุดวิดเจ็ต

ในแอป คุณสามารถสร้างคำขอให้ระบบปักหมุดวิดเจ็ตไว้ใน Launcher ที่รองรับโดยทำตามขั้นตอนต่อไปนี้

  1. ตรวจสอบว่าได้ประกาศวิดเจ็ตในไฟล์ Manifest ของแอปแล้ว

  2. เรียกใช้ 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);
}

ผู้ใช้ค้นพบและเพิ่มวิดเจ็ตของคุณผ่านเครื่องมือเลือกวิดเจ็ตหรือจากภายใน เมื่อฟังก์ชันการทำงานของวิดเจ็ตมีความเกี่ยวข้องมากที่สุด สำหรับข้อมูลเพิ่มเติม โปรดดู การค้นพบและการโปรโมต