如何改善ConnectionRequest的行为? [英] How to improve the behavior of ConnectionRequest?

查看:78
本文介绍了如何改善ConnectionRequest的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码:

public static Slider downloadStorageFile(String url, OnComplete<Integer> percentageCallback, OnComplete<String> filesavedCallback) {

        final String filename = getNewStorageFilename();

        ConnectionRequest cr = Rest.get(url).fetchAsBytes(response -> CN.callSerially(() -> filesavedCallback.completed(filename)));
        cr.setDestinationStorage(filename);

        Slider slider = new Slider();
        slider.addDataChangedListener((int type, int index) -> {
            CN.callSerially(() -> percentageCallback.completed(slider.getProgress()));
        });
        sliderBridgeMap.put(cr, slider);
        SliderBridge.bindProgress(cr, slider);

        return slider;
    }

基本上,您可以猜到,它异步下载文件,它提供两个会在EDT上运行的回调,并且会立即返回一个Slider,以跟踪下载进度。

Basically, as you can guess, it asynchronously downloads a file, it offers two callbacks that will run on the EDT and it immediately returns a Slider that keep track of the download progress.

在Android上,即使应用程序在后台。另一方面,在iOS上,该应用必须始终保持活动状态和前景。


  1. 是吗?

  1. Is it possible to complete the download even on iOS when the app goes to the background?

或者我可以在应用程序进入后台时完成下载吗?

Alternatively, can I get ConnectionRequest.retry(), which is automatically invoked by my network error handler when the app returns to foreground on iOS, to restart where the download arrived at, rather than restart it from scratch?

或者,我可以获得 ConnectionRequest.retry()吗? ),当应用程序在iOS上返回到前台时,它会由我的网络错误处理程序自动调用,以重新开始下载到的位置,而不是从头开始? p>

  • 更好的是,我能否同时获得第1点和第2点?

    Even better, can I get both points 1 and 2?


    推荐答案

    这在Android上也是有问题的,因为当系统受到限制时,操作系统可能会突然终止正在进行的下载。例如。在开发人员工具中,只需立即打开kill活动,即可在最小化应用程序的同时立即查看下载内容。

    This is problematic on Android too as the OS may suddenly kill your ongoing download when the system is constrained. E.g. in the developer tools just turn on the kill activities immediately and see your download die the moment you minimize the app.

    大多数设备不这样做,但是如果设备以省电模式运行,则可能会发生。

    Most devices don't do that but if a device is running in battery saving mode that might happen.

    解决方案是在进入后台时使用后台获取。问题在于,这无法提供常规前台下载所能提供的漂亮的UI,因此您需要选择要使用的UI。

    The solution is to use background fetch when going into the background. The problem is that this doesn't provide the nice sort of UI you would get with the regular foreground download so you need to pick and choose which one to use.

    在此处查看该类的JavaDoc: https://www.codenameone.com/javadoc/com/codename1/background/BackgroundFetch.html

    See the JavaDoc for the class here: https://www.codenameone.com/javadoc/com/codename1/background/BackgroundFetch.html

    关于该主题的过时博客文章: https://www.codenameone.com/blog /background-fetch.html

    And the slightly out of date blog post on the subject: https://www.codenameone.com/blog/background-fetch.html

    这篇关于如何改善ConnectionRequest的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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