PeriodicWorkRequest不起作用不重要什么

时间:2019-11-25 01:36:15

标签: android-workmanager workmanagers

PeriodicWorkRequest根本不起作用。如您所见,我已将重复间隔设置为至少15分钟。它入队,然后显示正在运行,仅此而已。什么都没发生,我什至已经等了30分钟!只是不起作用。但是,OneTimeWorkRequest可以正常工作,没有任何问题,但是在PeriodicWorkRequest上却没有运气。请在下面查看我的代码:

(function ()
{
    //Fade in delay for the background overlay (control timing here)
    $("#bkgOverlay").delay(4800).fadeIn(400);
  //Fade in delay for the popup (control timing here)
    $("#delayedPopup").delay(5000).fadeIn(400);

    //Hide dialouge and background when the user clicks the close button
    $("#btnClose").click(function (e)
    {
        HideDialog();
        e.preventDefault();
    });
})(); //call the function
//Controls how the modal popup is closed with the close button
function HideDialog()
{
    $("#bkgOverlay").fadeOut(400);
    $("#delayedPopup").fadeOut(300);
}

我也尝试过设置弹性间隔:

private fun processOneTimeADayNotifyReq() {
        val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(StepCountNotificationWorker::class.java, PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
            , TimeUnit.MINUTES)
            .build()
        val workManager: WorkManager = WorkManager.getInstance(this.context)
        workManager.enqueue(oneTimeADayReq)
        this.workerLiveData = workManager.getWorkInfoByIdLiveData(oneTimeADayReq.id)
        this.workerLiveData!!.observeForever(observeStepWorkerLiveData())
    }
    private fun observeStepWorkerLiveData(): Observer<WorkInfo> {
        return Observer {
            if (it?.state == null) {
                return@Observer
            } else {
                when (it.state) {
                    WorkInfo.State.RUNNING -> {
                        Log.i("Running: ", "running")
                    }
                    WorkInfo.State.BLOCKED -> {
                        Log.i("Blocked: ", "blocked")
                    }
                    WorkInfo.State.CANCELLED -> {
                        Log.i("Cancled: ", "canceled")
                    }
                    WorkInfo.State.ENQUEUED -> {
                        Log.i("Enqueued:", "enqueued")
                    }
                    WorkInfo.State.SUCCEEDED -> {
                        val outputData:Data = it.outputData
                        val calories: Int = outputData.getInt(Constants.STEP_COUNT_VALUE, 0)
                        Log.i("Calories: ", calories.toString())
                    }
                    WorkInfo.State.FAILED -> {

                    }
                    else -> {
                        return@Observer
                    }
                }
            }
        }
    }

这根本不起作用!我已经尝试使用版本2.3.0-alpha01和2.3.0-beta01。没运气。我使用的是Google Pixel 3a,targetSdkVersion是29。有人可以帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:2)

您将重复间隔设置为900000分钟,将其设置为15分钟,如果您使用PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLI然后使用TimeUnit.MILLISECONDS

val oneTimeADayReq: PeriodicWorkRequest = PeriodicWorkRequest.Builder(
        StepCountNotificationWorker::class.java, 
        PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
        TimeUnit.MILLISECONDS).build()

在执行结束时从RUNNINGENQUEUED的定期工作过渡,唯一的最终状态是CANCELLED