Android Work Manager - Does Work Manager will 100% ensure the background execution to be Completed?

巧了我就是萌 提交于 2019-12-06 01:55:35

Will the Work Manager run the process at background, and keep retrying the process even when there is no Internet Connection? and only complete and stop the process until there is an Internet Connection and complete the data upload?

It won't implicitly try to execute the work continuously and stop only if it was successful. It will depend on Result returned by doWork() or your Worker. If it returns RETRY, then the work will be retried with backoff specified in WorkRequest.Builder.setBackoffCriteria(BackoffPolicy, long, TimeUnit).

However, if you need something to be executed when there is internet connection, then you can specify the appropriate constraints. For network connectivity, you can set constraints as follows:

Constraints myConstraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();

 OneTimeWorkRequest mywork=
            new OneTimeWorkRequest.Builder(MyWorker.class)
 .setConstraints(myConstraints)
 .build();
 WorkManager.getInstance().enqueue(mywork);

WorkManager will ensure that your work is executed only if there is internet connection.

Will the Work Manager run the process at background, and keep retrying the process even when there is no Internet Connection? and only complete and stop the process until there is an Internet Connection and complete the data upload?

So because you've specified that a work manager requires Network Connection.

val constraints = Constraints.Builder()
                            .setRequiredNetworkType(NetworkType.CONNECTED).build()

val workRequest = OneTimeWorkRequest.Builder(RequestWorker::class.java)
                            .setConstraints(constraints)
                            .build()

The work request will not be triggered until there is a Network Connection, see it as the WorkManager listening for a change in NetworkState and once the Network is CONNECTED it starts processing your workRequest(doWork).

I must also add that while your phone is on AirPlane mode and your phone reboots with a pending work request, i currently don't think the work request is guaranteed to start processing immediately as you might expect, even when NetWork state is CONNECTED.

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