工槽管理器doWork()方法无法执行,但是onStartWork()被多次调用

时间:2018-08-19 10:58:29

标签: android android-jetpack android-workmanager

我正在尝试安排应用程序在1分钟后启动的时间,我需要做一些工作,不管应用程序是否仍在前台,但是我需要这样做,但是我不知道为什么我的doWork方法无法运行。但是onStart方法称为多次

private void sacduleThePaddingNotifaction() {
    OneTimeWorkRequest postNotationWithDelay =
            new OneTimeWorkRequest.Builder(PostNotificationWork.class)
                    .setInitialDelay(1,TimeUnit.MINUTES)
                    .build();
    WorkManager.getInstance().enqueue(postNotationWithDelay);


}

我的工人阶级

public class PostNotificationWork extends Worker {
    @Override
    public void onStartWork(@NonNull WorkFinishedCallback callback) {
        Log.d("PostNotificationWork: ","onStartWork");//this called many times, some time 4 time 
    }

    @NonNull
    @Override
    public Result doWork() {

        Log.d("PostNotificationWork: ","Posting notification");
        return Result.SUCCESS;
    }
}

我正在使用def work_version = "1.0.0-alpha07"

2 个答案:

答案 0 :(得分:0)

  • 尝试更新到此版本 def work_version = "1.0.0-alpha06" implementation "android.arch.work:work-runtime:$work_version"
  • 删除 @Override public void onStartWork(@NonNull WorkFinishedCallback callback) { Log.d("PostNotificationWork: ","onStartWork");//this called many times, some time 4 time }
  • 最后再试一次

答案 1 :(得分:0)

请不要覆盖onStartWork()。即使您需要拨打super.onStartWork()。我们仍在研究NonBlockingWorker API的人体工程学。