android 使用WorkManager替换Service

Service在8.0之后需要做各种适配保活,还有通知栏的通知要处理,不想麻烦的朋友可以考虑使用WorkManager替换Service

val constraints = Constraints.Builder()
       .setRequiredNetworkType(NetworkType.CONNECTED)
       .build()
   val myWorkRequest = OneTimeWorkRequest.Builder(AutoWallpaperWork::class.java)
       .setConstraints(constraints)
       .build()

   WorkManager.getInstance(instance).enqueue(myWorkRequest)
class AutoWallpaperWork(var context: Context, params: WorkerParameters) : Worker(context, params) {

    override fun doWork(): Result {
  // 返回 Result.success() 表示任务执行成功
    return Result.success()
}

}

你可能感兴趣的:(不用找系列,android,kotlin,android,studio)