Android Work Manager

编程入门 行业动态 更新时间:2024-10-24 14:24:40
本文介绍了Android Work Manager-Work Manager是否会100%确保完成后台执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

根据我从文档> developer.android/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

本文发布于:2023-11-27 12:00:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   Work   Manager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!