Android Worker多次执行作业 [英] Android Worker execute the job multiple times

查看:74
本文介绍了Android Worker多次执行作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码安排每15分钟执行一次后台作业.

I use the following code to schedule a background job execute every 15 mins.

WorkManager workManager = WorkManager.getInstance();
PeriodicWorkRequest ulpBackup;

ulpBackup = new PeriodicWorkRequest
    .Builder(Ulp.class, 15, TimeUnit.MINUTES)
    .addTag(activity.getString(R.string.job_tag))
    .build();
workManager.enqueue(ulpBackup);

这是UlpBackup.class

And here is the UlpBackup.class

public class UlpBackup extends Worker {
    private Integer responseCounter = 0;

    public UlpBackup() {}

    @NonNull
    @Override
    public Result doWork() {

        Log.d(logTag, "Starting periodic backup job";   
        final CountDownLatch countDownLatch = new CountDownLatch(1);

        /** Read from local database and upload to firestore **/

        localdb.setAPIListener(new APIListener() {
            @Override
            public void OnSuccess() {
                responseCounter++;
                if (responseCounter == 5) {
                    countDownLatch.countDown();
                }
            }

            @Override
            public void OnFailure() {
                responseCounter++;
                if (responseCounter == 5) {
                    countDownLatch.countDown();
                }
            }
        });

        localdb.sync();

        try {
            countDownLatch.await(300, TimeUnit.SECONDS);
        } catch (Exception exception) {
            Log.e(logTag, "Error in user list backup job " + exception.getMessage());
            return Result.FAILURE;
        }

        Log.e(logTag, "Ulp backup completed");
        return Result.SUCCESS;
}

上面的代码可以正常工作,并且大约按预期每15分钟执行一次工作.我唯一不了解的是,每一次工作执行多次,有人可以解释为什么以及如何避免吗?

The above code works fine and the job happened roughly every 15 minutes as expected. The only thing that I do not understand is that every time the job execute multiple times, can someone explain why and how can i avoid it?

从日志中:

09-15 23:33:37.514 8190-8410: Starting periodic backup job
09-15 23:33:37.520 8190-8414: Starting periodic backup job
09-15 23:33:37.561 8190-8412: Starting periodic backup job
09-15 23:33:37.568 8190-8413: Starting periodic backup job
...
...
09-15 23:33:38.183 8190-8414: Ulp backup completed
09-15 23:33:39.164 8190-8412: Ulp backup completed
09-15 23:33:39.580 8190-8413: Ulp backup completed
09-15 23:38:37.517 8190-8410: Ulp backup completed

推荐答案

您可以使用以下方法: enqueueUniquePeriodicWork(),并像这样实现:

You can use this following method : enqueueUniquePeriodicWork(), and implement like that :

WorkManager.getInstance().enqueueUniquePeriodicWork("YOUR_TAG", ExistingPeriodicWorkPolicy.KEEP, workRequest);

请注意,第二个参数是键.枚举可以是 KEEP REPLACE

Please notice that the second parameter is the key. The enum can be KEEP or REPLACE

这篇关于Android Worker多次执行作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆