正如我们在Google问题跟踪器中
这里 看到的:
不幸的是,一些设备将从“最近使用”菜单中终止应用程序视为强制停止。 默认的Android操作系统不会这样做。当应用程序被强制停止时,它无法执行作业、接收警报或广播等操作。因此,我们无法解决这个问题——问题出在操作系统上,并且没有解决方法。
因此,您需要一个 Service 来保持您的应用程序活动状态。并且当该 Service 被终止(无论原因何在),它应该再次启动并初始化您的 worker,以确保其执行并保持 worker 任务的活动状态。以下是使用 STICKY IntentService 实现此想法的示例代码。
WallpaperService.kt
import android.app.IntentService
import android.app.Service
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import java.util.*
import java.util.concurrent.TimeUnit
class WallpaperService : IntentService("WallpaperService") {
override fun onHandleIntent(intent: Intent?) {
intent?.apply {
when (intent.action) {
ACTION_SETUP_WORKER -> {
setupWorker()
}
}
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
return Service.START_STICKY
}
private fun setupWorker() {
val calendar = Calendar.getInstance()
val currentTime = calendar.timeInMillis
calendar.set(Calendar.HOUR_OF_DAY, 9)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
calendar.set(Calendar.MILLISECOND, 0)
calendar.add(Calendar.DAY_OF_MONTH, 1)
val tomorrowTime = calendar.timeInMillis
val timeDiffBetweenNowAndTomorrow = tomorrowTime - currentTime
Log.i("WallpaperService", "************ Tomorrow date is ${calendar.timeInMillis}")
Log.i("WallpaperService", "************ Difference between now and tomorrow $timeDiffBetweenNowAndTomorrow")
val randomWorkRequest = OneTimeWorkRequestBuilder<RandomWallpaperWorker>()
.setInitialDelay(timeDiffBetweenNowAndTomorrow, TimeUnit.MILLISECONDS)
.build()
WorkManager.getInstance().enqueue(randomWorkRequest)
}
companion object {
const val ACTION_SETUP_WORKER = "ACTION_SETUP_WORKER"
fun setupWorker(context: Context) {
val intent = Intent(context, WallpaperService::class.java)
intent.action = ACTION_SETUP_WORKER
context.startService(intent)
}
}
}
RandomWallpaperWorker.kt
import android.content.Context
import android.util.Log
import androidx.work.Worker
import androidx.work.WorkerParameters
class RandomWallpaperWorker(val context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
Log.e("RandomWallpaperWorker", "***************** DONE!" )
WallpaperService.setupWorker(context)
return Result.SUCCESS
}
}
MainActivity.kt
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
WallpaperService.setupWorker(applicationContext)
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aminography.workerapplication">
<application ... >
...
<service android:name=".WallpaperService" android:enabled="true"/>
</application>
</manifest>
OneTimeWorkRequestBuilder<T>
是您自定义的类吗?您是否想要消除标准构建器,因为需要在参数中传递类类型? - aminography