รองรับผู้ปฏิบัติงานระยะยาว

WorkManager มีการรองรับในตัวสำหรับ Worker ที่ทำงานเป็นเวลานาน ในกรณีเช่นนี้ WorkManager สามารถส่งสัญญาณไปยังระบบปฏิบัติการว่าควรรักษากระบวนการให้ทำงานต่อไปหากเป็นไปได้ขณะที่งานนี้กำลังดำเนินการอยู่ Worker เหล่านี้สามารถทำงานได้นานกว่า 10 นาที ตัวอย่างกรณีการใช้งานสำหรับฟีเจอร์ใหม่นี้ ได้แก่ การอัปโหลดหรือดาวน์โหลดหลายรายการพร้อมกัน (ซึ่งไม่สามารถแบ่งเป็นกลุ่มได้) การประมวลผลโมเดล ML ในเครื่อง หรือภารกิจที่สำคัญต่อผู้ใช้แอป

WorkManager จะจัดการและเรียกใช้บริการที่ทำงานอยู่เบื้องหน้าในนามของคุณเพื่อเรียกใช้ WorkRequest และแสดงการแจ้งเตือนที่กำหนดค่าได้

ตอนนี้ ListenableWorker รองรับ setForegroundAsync() API และ CoroutineWorker รองรับ setForeground() API ที่ระงับได้ API เหล่านี้ช่วยให้นักพัฒนาซอฟต์แวร์ระบุได้ว่า WorkRequest นี้สำคัญ (จากมุมมองของผู้ใช้) หรือใช้เวลานาน

ตั้งแต่ 2.3.0-alpha03 เป็นต้นไป WorkManager ยังให้คุณสร้าง PendingIntent ซึ่งใช้เพื่อยกเลิก Worker ได้โดยไม่ต้องลงทะเบียนคอมโพเนนต์ Android ใหม่โดยใช้ API createCancelPendingIntent() แนวทางนี้มีประโยชน์อย่างยิ่งเมื่อใช้กับ setForeground() API ซึ่งสามารถใช้เพื่อเพิ่มการดำเนินการแจ้งเตือนเพื่อยกเลิก Worker

การสร้างและจัดการเวิร์กเกอร์ที่ทำงานเป็นเวลานาน

ใช้ CoroutineWorker และเมธอด setForeground() ที่ระงับเพื่อกำหนดค่าผู้ปฏิบัติงานที่ทำงานเป็นเวลานานและอัปเดต Notification ที่กำลังดำเนินอยู่

class DownloadWorker(context: Context, parameters: WorkerParameters) :
    CoroutineWorker(context, parameters) {

    private val notificationManager =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as
                NotificationManager

    override suspend fun doWork(): Result {
        val inputUrl = inputData.getString(KEY_INPUT_URL)
            ?: return Result.failure()
        val outputFile = inputData.getString(KEY_OUTPUT_FILE_NAME)
            ?: return Result.failure()
        // Mark the Worker as important.
        val progress = "Starting Download"
        setForeground(createForegroundInfo(progress))
        download(inputUrl, outputFile)
        return Result.success()
    }

    private fun download(inputUrl: String, outputFile: String) {
        // Downloads a file and updates bytes read.
        // Calls setForeground() periodically when it needs to update
        // the ongoing Notification.
    }
    // Creates an instance of ForegroundInfo which can be used to update the
    // ongoing notification.
    private fun createForegroundInfo(progress: String): ForegroundInfo {
        val id = applicationContext.getString(R.string.notification_channel_id)
        val title = applicationContext.getString(R.string.notification_title)
        val cancel = applicationContext.getString(R.string.cancel_download)
        // This PendingIntent can be used to cancel the worker.
        val intent = WorkManager.getInstance(applicationContext)
            .createCancelPendingIntent(getId())

        // Create a Notification channel if necessary.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel()
        }

        val notification = NotificationCompat.Builder(applicationContext, id)
            .setContentTitle(title)
            .setTicker(title)
            .setContentText(progress)
            .setSmallIcon(R.drawable.ic_work_notification)
            .setOngoing(true)
            // Add the cancel action to the notification which can
            // be used to cancel the worker.
            .addAction(android.R.drawable.ic_delete, cancel, intent)
            .build()

        return ForegroundInfo(notificationId, notification)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createChannel() {
        // Create a Notification channel.
    }

    companion object {
        const val KEY_INPUT_URL = "KEY_INPUT_URL"
        const val KEY_OUTPUT_FILE_NAME = "KEY_OUTPUT_FILE_NAME"
    }
}

เพิ่มประเภทบริการที่ทำงานอยู่เบื้องหน้าให้กับ Worker ที่ทำงานเป็นเวลานาน

หากแอปกำหนดเป้าหมายเป็น Android 14 (ระดับ API 34) ขึ้นไป คุณต้องระบุประเภทบริการที่ทำงานอยู่เบื้องหน้าสำหรับ Worker ที่ทำงานเป็นเวลานานทั้งหมด หากแอปกำหนดเป้าหมายเป็น Android 10 (API ระดับ 29) ขึ้นไปและมี Worker ที่ทำงานเป็นเวลานานซึ่งต้องเข้าถึงตำแหน่ง ให้ระบุว่า Worker ใช้ประเภทบริการที่ทำงานอยู่เบื้องหน้าเป็น location

หากแอปกำหนดเป้าหมายเป็น Android 11 (ระดับ API 30) ขึ้นไปและมีเวิร์กเกอร์ที่ทำงานเป็นเวลานานซึ่งต้องเข้าถึงกล้องหรือไมโครโฟน ให้ประกาศประเภทบริการที่ทำงานอยู่เบื้องหน้าเป็น camera หรือ microphone ตามลำดับ

หากต้องการเพิ่มประเภทบริการที่ทำงานอยู่เบื้องหน้าเหล่านี้ ให้ทำตามขั้นตอนที่อธิบายไว้ในส่วนต่อไปนี้

ประกาศประเภทบริการที่ทำงานอยู่เบื้องหน้าในไฟล์ Manifest ของแอป

ประกาศประเภทบริการที่ทำงานอยู่เบื้องหน้าของ Worker ในไฟล์ Manifest ของแอป ในตัวอย่างต่อไปนี้ ผู้ปฏิบัติงานต้องมีสิทธิ์เข้าถึงตำแหน่งและไมโครโฟน

AndroidManifest.xml

<service
   android:name="androidx.work.impl.foreground.SystemForegroundService"
   android:foregroundServiceType="location|microphone"
   tools:node="merge" />

ระบุประเภทบริการที่ทำงานอยู่เบื้องหน้าขณะรันไทม์

เมื่อเรียกใช้ setForeground() หรือ setForegroundAsync() โปรดระบุประเภทบริการที่ทำงานอยู่เบื้องหน้า

MyLocationAndMicrophoneWorker

private fun createForegroundInfo(progress: String): ForegroundInfo {
    // ...
    return ForegroundInfo(
        NOTIFICATION_ID,
        notification,
        FOREGROUND_SERVICE_TYPE_LOCATION or
            FOREGROUND_SERVICE_TYPE_MICROPHONE
    )
}