Android Work Manager-Work Manager是否会100%确保完成后台执行? [英] Android Work Manager - Does Work Manager will 100% ensure the background execution to be Completed?

查看:425
本文介绍了Android Work Manager-Work Manager是否会100%确保完成后台执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我从文档> https://developer.android.com/topic读到的内容/libraries/architecture/workmanager

它说:

即使您的应用被强制退出或 设备重新启动.

The task is still guaranteed to run, even if your app is force-quit or the device is rebooted.

这意味着,无论如何,在后台执行的任务都会100%执行到完成为止.

So which mean, the execution that being at the background will 100% be execute until it complete no matter what?

例如:

一个具有执行Work Manager实施的应用程序的按钮,该按钮可将数据上传到在线数据库,但需要Internet连接才能上传数据.因此,我的应用程序当前处于离线模式,然后单击按钮.

An Apps have Button that perform Work Manager Implementation that upload data to an Online Database but it required Internet Connection to upload the data. So, my Apps is currently in Offline Mode and I click the Button.

不确定性

工作管理器是否会在后台运行该过程,并且即使没有Internet连接也会继续重试该过程?并且仅完成并停止该过程,直到建立Internet连接并完成数据上传?

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?

推荐答案

工作经理将在后台运行该流程,并继续重试 即使没有Internet连接,该过程也如何?而且只有 完成并停止该过程,直到建立Internet连接为止 并完成数据上传?

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?

它不会隐式地尝试连续执行工作并仅在成功后才停止.这将取决于doWork()或您的Worker返回的结果.如果返回RETRY,则将使用WorkRequest.Builder.setBackoffCriteria(BackoffPolicy, long, TimeUnit)中指定的退避重试该工作.

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将确保只有在连接互联网的情况下才能执行您的工作.

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

这篇关于Android Work Manager-Work Manager是否会100%确保完成后台执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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