How to detect upload/download transfer rate in Android?

后端 未结 3 1468
借酒劲吻你
借酒劲吻你 2020-12-30 17:20

I am working on an app which uploads a large amount of data. I want to determine the transfer rate of the upload, to show in a notification.

  • One post suggests
3条回答
  •  悲&欢浪女
    2020-12-30 18:07

    I am talking in the context of your app since this makes it easier to capture the real time speed of your uploaded data. You don't need any extra libraries or sdk api's.

    You are presumably uploading the data in chunks to the server. So

    a) You know the data size of each packet
    b) You know the start time before sending the packet / before sending multiple packets
    c) You know the end time of xy packets by the server response e.g. status 200

    With that you have all parameters to calculate the upload speed

    double uploadSpeed = packet.size / (endTime - startTime) // time * 1000 to have it in seconds

    EDIT:

    Since you are using MultiPart from OkHttp you can monitor the amount of bytes uploaded. Tracking progress of multipart file upload using OKHTTP. You would replace packet.size with the current uploaded amount and the endTime would be an interval of xy seconds.

提交回复
热议问题