define netspeed

pull/102/head
watson1982 3 years ago committed by GitHub
parent a2ec375c4e
commit b0482f8aae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      player/src/main/java/xyz/doikki/videoplayer/util/PlayerUtils.java

@ -9,6 +9,7 @@ import android.content.res.Resources;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.TypedValue;
@ -34,7 +35,8 @@ import java.util.Locale;
*/
public final class PlayerUtils {
private static long lastTotalRxBytes;
private static long lastTimeStamp;
private PlayerUtils() {
}
@ -286,4 +288,30 @@ public final class PlayerUtils {
}
return result;
}
}
public static long getNetSpeed(Context context) {
//这里的context是获取getApplicationContext的
if (context == null) {
return 0;
}
//先使用getUidRxBytes方法获取该进程总接收量,如果没获取到就把当前接收数据总量设置为0,否则就获取接收的总流量并转为kb
long nowTotalRxBytes = TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes());//转为KB
//记录当前的时间
long nowTimeStamp = System.currentTimeMillis();
//上一次记录的时间-当前记录时间算出两次记录的时间差
long calculationTime = (nowTimeStamp - lastTimeStamp);
//如果时间差不变,直接返回0
if (calculationTime == 0) {
return calculationTime;
}
//两次的数据接收量的差除以两次数据接收的时间,就计算网速了。这边的时间差是毫秒,咱们需要转换成秒。
long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / calculationTime);
//当前时间存到上次时间这个变量,供下次计算用
lastTimeStamp = nowTimeStamp;
//当前总接收量存到上次接收总量这个变量,供下次计算用
lastTotalRxBytes = nowTotalRxBytes;
return speed;
}
}
Loading…
Cancel
Save