Work Manager未能按设置的初始延迟调度Work

5

所以我有一个工作程序,必须从预定时间的第二天开始运行。因此,如果工作在今晚8点激活,则需要在明天上午9点执行工作。因此,我使用OneTimeWorkRequestsetInitialDelay()

以下是代码:

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)

但我检查了一下,第二天早上我醒来时,工作并没有执行。 为什么它没有被调度?我计算明天的时间戳的方式有问题吗?


OneTimeWorkRequestBuilder<T> 是您自定义的类吗?您是否想要消除标准构建器,因为需要在参数中传递类类型? - aminography
这只是与工作管理器依赖项一起提供的Kotlin快捷方式。您可以在结尾处添加“-ktx”以获得Kotlin友好的语法,并使用每个架构组件。 - Sriram R
干得好! - aminography
1个回答

2
正如我们在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)
        // 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>

当我按照自己的方式进行时,工作安排可以在日志中看到。此外,我将ID保存到共享首选项中,以便确认已安排工作。当使用IntentService进行工作调度时,有何不同?因为一旦安排了工作,无论从哪里运行,它都会按预期运行,对吗?从IntentService进行调度与从Fragment进行调度有何区别? - Sriram R
WorkManager 在应用层工作,而不是操作系统。我的意思是它在您的应用程序进程中运行,因此当进程被杀死时,worker manager 对象及其计划的 worker 将被完全从内存中删除。如果您将任务安排在 10 秒钟后而不是明天早上 9 点钟,请清除最近使用应用程序,则会发现您的计划 worker 不会运行。以上服务避免了长时间杀死应用程序。在其终止后,它重新启动并重新安排您的任务,并确保 worker 任务仍驻留在内存中。 - aminography
@SriramR:你测试过了吗? - aminography
是的,有时候工人的工作情况符合预期,但有时候不是。我正在尝试调试它。 - Sriram R
第一次尝试时它按预期工作。之后,它就不再工作了,工作程序也没有执行。第一次为了测试目的,我将初始延迟设置为从我开始尝试算起的10分钟。下一次我尝试在第二天早上9点进行,但是那次并没有成功。 - Sriram R
请问您能告诉我您的手机品牌和操作系统规格吗? - aminography

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接