zoukankan      html  css  js  c++  java
  • android EventBus在下载上传速度过快5M/S时无法更新Dialog UI的下载进度

    现象:能接收到,打印的进度文字也更新了,但显示出来的就是不变

    只显示了最后一个值

    后面经过多次调试,发现限制下载速度后能够更新显示的进度

    下载速度限制使用的

      Thread.sleep(1);时无法更新进度

    Thread.sleep(5);时,可以更新进度
    下载进度使用RxBus post到界面更新
    最终主管确定限定下载速度不合理,因为限定下载速度与不限定,经过测试占用的内存都在180M左右
    只限制
    RxBus post 的次数 每隔 100 ms发送一次post
     1 public class ProgressResponseBody extends ResponseBody {
     2     private ResponseBody responseBody;
     3 
     4     private BufferedSource bufferedSource;
     5     private String tag;
     6 
     7     public ProgressResponseBody(ResponseBody responseBody) {
     8         this.responseBody = responseBody;
     9     }
    10 
    11     public ProgressResponseBody(ResponseBody responseBody, String tag) {
    12         this.responseBody = responseBody;
    13         this.tag = tag;
    14     }
    15 
    16     @Override
    17     public MediaType contentType() {
    18         return responseBody.contentType();
    19     }
    20 
    21     @Override
    22     public long contentLength() {
    23         return responseBody.contentLength();
    24     }
    25 
    26     @Override
    27     public BufferedSource source() {
    28         if (bufferedSource == null) {
    29             bufferedSource = Okio.buffer(source(responseBody.source()));
    30         }
    31         return bufferedSource;
    32     }
    33 
    34     long oldTime = System.currentTimeMillis();
    35 
    36     private Source source(Source source) {
    37         return new ForwardingSource(source) {
    38             long bytesReaded = 0;
    39 
    40             @Override
    41             public long read(Buffer sink, long byteCount) throws IOException {
    42                 long bytesRead = super.read(sink, byteCount);
    43                 bytesReaded += bytesRead == -1 ? 0 : bytesRead;
    44 //                try {
    45 //                    Thread.sleep(1);
    46 //                } catch (InterruptedException e) {
    47 //                    e.printStackTrace();
    48 //                }
    49                 long currntTime = System.currentTimeMillis();
    50 //                KLog.i("------------------------>>>  return "+(currntTime-oldTime));
    51                 if (currntTime - oldTime < 100) {
    52 //                    KLog.i("------------------------>>>  return");
    53                 } else {
    54                     oldTime = currntTime;
    55 //                    KLog.i("------------------------>>>  post data");
    56                     //使用RxBus的方式,实时发送当前已读取(上传/下载)的字节数据
    57                     DownLoadStateBean infoBena = new DownLoadStateBean(contentLength(), bytesReaded, tag);
    58 //                KLog.i("--------->>> 总大小为  " + infoBena.getTotal() + " 一次读取的大小为 " + infoBena.getBytesLoaded());
    59                     RxBus.getDefault().post(infoBena);
    60                 }
    61                 return bytesRead;
    62             }
    63         };
    64     }
    65 }
    View Code
     


     
  • 相关阅读:
    微信小程序-上传多张图片加进度条(支持预览、删除)
    php中120个内置函数
    angular6 NgModule中定义模块module
    Aliasing input/output properties
    angular6 Can't bind to 'zzst' since it isn't a known property of
    [转]DOM 中 Property 和 Attribute 的区别
    Angular6
    [转]VirtualBox 修改UUID实现虚拟硬盘复制
    pthread_create如何传递两个参数以上的参数
    linux 线程操作问题undefined reference to 'pthread_create'的解决办法(cmake)
  • 原文地址:https://www.cnblogs.com/caosq/p/12887032.html
Copyright © 2011-2022 走看看