因此,我有一个工人,必须在第二天运行时,它的计划。因此,如果工作在今天晚上8点启动,那么我需要在第二天上午9点执行这项工作。所以我使用的是OneTimeWorkRequest和setInitialDelay()。
这是代码
val currentTime = System.currentTimeMillis()
// calculate the timestamp for next dat 9AM
val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY, 9)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
// next day
calendar.add(Calendar.DAY_OF_MONTH, 1)
val tomorrowTime = calendar.timeInMillis
val timeDiffBetweenNowAndTomorrow = tomorrowTime - currentTime
Timber.i("Tomorrow date is ${calendar.timeInMillis}")
Timber.i("Difference between now and tomorrow ${timeDiffBetweenNowAndTomorrow}")
val randomWorkRequest = OneTimeWorkRequestBuilder<RandomWallpaperWorker>()
.setInitialDelay(timeDiffBetweenNowAndTomorrow, TimeUnit.MILLISECONDS)
.build()
WorkManager.getInstance().enqueue(randomWorkRequest)但是第二天醒来的时候,我检查了一下,结果工作没有执行。为什么没安排好呢?我计算第二天的时间戳有什么问题吗?
发布于 2018-11-06 22:17:23
正如我们在Google的问题跟踪器中看到的这里:
不幸的是,一些设备在最近的菜单中将应用程序实现为强制停止。Android股票并不能做到这一点。当一个应用程序被强制停止时,它不能执行作业、接收警报或广播等等。因此,不幸的是,我们解决它是不可行的-问题在于操作系统,没有解决办法。
因此,您需要一个Service来维护您的应用程序。另外,当Service被终止时(不管原因是什么),它应该重新启动并初始化您的工作人员,以确保它的执行并保持工作任务的存活。下面是使用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)
// Define service as sticky so that it stays in background
return Service.START_STICKY
}
private fun setupWorker() {
val calendar = Calendar.getInstance()
val currentTime = calendar.timeInMillis
// for removing from recent apps test
// calendar.add(Calendar.SECOND, 10)
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 {
// Do what you want here...
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>https://stackoverflow.com/questions/53097297
复制相似问题