支持长时间运行的 Worker

WorkManager 内置支持长时间运行的 Worker。在这种情况下,如果可能,WorkManager 可以向操作系统发出信号,表明在执行此工作时应尽可能保持进程存活。这些 Worker 的运行时间可以超过 10 分钟。此新功能的用例示例包括批量上传或下载(无法分块)、在本地处理 ML 模型或对应用用户至关重要的任务。

在后台,WorkManager 代表您管理并运行前台服务以执行WorkRequest,同时还显示可配置的通知。

ListenableWorker 现在支持setForegroundAsync() API,而CoroutineWorker 支持挂起setForeground() API。这些 API 允许开发者指定此WorkRequest对用户而言很重要长时间运行

2.3.0-alpha03开始,WorkManager 还允许您创建一个PendingIntent,该对象可用于取消 Worker,而无需使用createCancelPendingIntent() API注册新的 Android 组件。这种方法在与setForegroundAsync()setForeground() API结合使用时特别有用,后者可用于添加取消Worker的通知操作。

创建和管理长时间运行的 Worker

您将使用略微不同的方法,具体取决于您是在 Kotlin 还是 Java 中进行编码。

Kotlin

Kotlin 开发者应使用CoroutineWorker。无需使用setForegroundAsync(),您可以使用该方法的挂起版本setForeground()

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"
   }
}

Java

使用ListenableWorkerWorker的开发者可以调用setForegroundAsync() API,该 API 返回ListenableFuture<Void>。您还可以调用setForegroundAsync()来更新正在进行的Notification

这是一个长时间运行的 Worker 的简单示例,它下载文件。此 Worker 会跟踪进度以更新正在进行的Notification,该通知显示下载进度。

public class DownloadWorker extends Worker {
   private static final String KEY_INPUT_URL = "KEY_INPUT_URL";
   private static final String KEY_OUTPUT_FILE_NAME = "KEY_OUTPUT_FILE_NAME";

   private NotificationManager notificationManager;

   public DownloadWorker(
       @NonNull Context context,
       @NonNull WorkerParameters parameters) {
           super(context, parameters);
           notificationManager = (NotificationManager)
               context.getSystemService(NOTIFICATION_SERVICE);
   }

   @NonNull
   @Override
   public Result doWork() {
       Data inputData = getInputData();
       String inputUrl = inputData.getString(KEY_INPUT_URL);
       String outputFile = inputData.getString(KEY_OUTPUT_FILE_NAME);
       // Mark the Worker as important
       String progress = "Starting Download";
       setForegroundAsync(createForegroundInfo(progress));
       download(inputUrl, outputFile);
       return Result.success();
   }

   private void download(String inputUrl, String outputFile) {
       // Downloads a file and updates bytes read
       // Calls setForegroundAsync(createForegroundInfo(myProgress))
       // periodically when it needs to update the ongoing Notification.
   }

   @NonNull
   private ForegroundInfo createForegroundInfo(@NonNull String progress) {
       // Build a notification using bytesRead and contentLength

       Context context = getApplicationContext();
       String id = context.getString(R.string.notification_channel_id);
       String title = context.getString(R.string.notification_title);
       String cancel = context.getString(R.string.cancel_download);
       // This PendingIntent can be used to cancel the worker
       PendingIntent intent = WorkManager.getInstance(context)
               .createCancelPendingIntent(getId());

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
           createChannel();
       }

       Notification notification = new NotificationCompat.Builder(context, id)
               .setContentTitle(title)
               .setTicker(title)
               .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 new ForegroundInfo(notificationId, notification);
   }

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

向长时间运行的 Worker 添加前台服务类型

如果您的应用面向 Android 14(API 级别 34)或更高版本,则必须为所有长时间运行的工作器指定前台服务类型。如果您的应用面向 Android 10(API 级别 29)或更高版本,并且包含需要访问位置的长运行工作器,则表明该工作器使用`location`类型的前台服务

如果您的应用面向 Android 11(API 级别 30)或更高版本,并且包含需要访问相机或麦克风的长运行工作器,则分别声明cameramicrophone前台服务类型。

要添加这些前台服务类型,请完成以下部分中描述的步骤。

在应用清单中声明前台服务类型

在您的应用清单中声明工作器的前台服务类型。在下面的示例中,工作器需要访问位置和麦克风

AndroidManifest.xml

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

在运行时指定前台服务类型

调用`setForeground()`或`setForegroundAsync()`时,请确保指定前台服务类型

MyLocationAndMicrophoneWorker

Kotlin

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

Java

@NonNull
private ForegroundInfo createForegroundInfo(@NonNull String progress) {
   // Build a notification...
   Notification notification = ...;
   return new ForegroundInfo(NOTIFICATION_ID, notification,
           FOREGROUND_SERVICE_TYPE_LOCATION | FOREGROUND_SERVICE_TYPE_MICROPHONE);
}