WorkManager OneTimeWorkRequest InitialDelay works after twise the time set

微笑、不失礼 提交于 2020-01-11 07:19:14

问题


UPDATE (21-nov-2019)

WorkManager.getInstance(activity).enqueueUniquePeriodicWork("test_work",
                                                ExistingPeriodicWorkPolicy.KEEP,
                                                PeriodicWorkRequest.Builder(MyWorker::class.java,
                                                        15, TimeUnit.MINUTES)
                                                        .build())

Now I'm using PeriodicWork and now doWork() called twice even I cancelled uniquettask at first attempt.I'm getting Notification twice and Checked in Log also It's called twice.

Note: It's happen only first time and sometime second time also but not getting third time twice.

override fun doWork(): Result {


            if (checkTaskFinished()) {
                Logger.e("checkXXX----Hurrayyyy  ☻♥☺")
                val notificationUtils: NotificationUtils = NotificationUtils(applicationContext)
                notificationUtils.showNotificationMessage("Readddd", "rwaeaeae.", 2)


                WorkManager.getInstance(context).cancelUniqueWork("test_work")
            }

    return Result.success()

}

Library I am using : implementation 'androidx.work:work-runtime-ktx:2.2.0'


OLD

I'm facing problem with WorkManager OneTimeWorkRequest setInitialDelay.

It's work fine when app is in forground or in recent list. But When I remove App from recent list everything is messed up.

What I want to achieve ? - I want send notification to user after few hours when some task is pending ,so after some R&D start work using WorkManager because of It's ability to Schedule Tasks without background service limitation.

Now below is code snippet which work till app is not removed from recent:

    Constraints constraints = new Constraints.Builder()
                                .setRequiresBatteryNotLow(true)
                                .build();

    final OneTimeWorkRequest simpleRequest = new OneTimeWorkRequest.Builder(MyWorker.class)
            .setInitialDelay(3, TimeUnit.MINUTES)
            .setConstraints(constraints)
            .addTag("simple_work")
            .build();

    WorkManager workManager = WorkManager.getInstance();

    workManager.beginUniqueWork("simple_work", ExistingWorkPolicy.KEEP, simpleRequest).enqueue();

Worker class

    public class MyWorker extends Worker {
    private NotificationUtils notificationUtils;
    public static final String EXTRA_OUTPUT_MESSAGE = "output_message";

    public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {

        notificationUtils = new NotificationUtils(getApplicationContext());
        notificationUtils.showNotificationMessage("TITLE", "This is a MESSEAGE",2);

        Data output = new Data.Builder()
                .putString(EXTRA_OUTPUT_MESSAGE, "I have come from MyWorker!")
                .build();

        setOutputData(output);

        return Result.SUCCESS;
    }


}

Problem is After Remove It from recent list It's send notification but after double time which I set. for example I set setInitialDelay 5 minutes but It's work after 10 minutes.

So ,Please guide me what I do wrong or It's not for schedule task at specific time which I set for once. I don't want to keep repeat work after It's done. I'm create just new after finish first OneTimeWorkRequest so It has to work fine as per documented beginUniqueWork.

Library I am using : implementation "android.arch.work:work-runtime:1.0.0-alpha11"

English isn't my native language so pardon me for grammatically mistake:)


回答1:


You problem is not clear though please update latest work manager lib to android.arch.work:work-runtime:1.0.0-beta03

And you can explain me why do u need to enque the unique work ?

workManager.beginUniqueWork("simple_work", ExistingWorkPolicy.KEEP, simpleRequest).enqueue();

try to replace it with workManager.enque(simpleRequest); and let me know if worked



来源:https://stackoverflow.com/questions/53301908/workmanager-onetimeworkrequest-initialdelay-works-after-twise-the-time-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!