WorkManager OneTimeWorkRequest InitialDelay works after twise the time set
Asked Answered
A

1

2

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:)

Alcus answered 14/11, 2018 at 13:56 Comment(7)
have you found an answer to your question?Proposal
It's some experiment in that project so we dropped that and I didn't try after that so ...Alcus
@Andrea Hi I just started on that work again and still getting same problem I'm using now implementation 'androidx.work:work-runtime-ktx:2.2.0' . I did update question.Alcus
I have to downgrade the version to 2.1.0 in order for it to workProposal
Any updates on this? Can't upgrade WorkManager from 2.0.1 to 2.1.0, nor 2.2.0, nor 2.3.4 for this issueAparicio
Sorry that project no longer in production so I'm not working on workmanager. Hope someone give you update :)Alcus
Still got the same problem with latest version androidx.work:work-runtime:2.7.1 Did you got the solution?Adamsun
R
0

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

Rasure answered 11/2, 2019 at 14:6 Comment(1)
Not working even in latest implementation 'androidx.work:work-runtime-ktx:2.2.0'Alcus

© 2022 - 2024 — McMap. All rights reserved.