Update Traffic

pull/557/head
okjack 1 year ago
parent b1d765d85a
commit fc186e78dc
  1. 2
      app/src/main/java/com/fongmi/android/tv/Constant.java
  2. 17
      app/src/main/java/com/fongmi/android/tv/utils/Traffic.java

@ -6,7 +6,7 @@ public class Constant {
//控件隱藏時間
public static final int INTERVAL_HIDE = 5 * 1000;
//網路偵測間隔
public static final int INTERVAL_TRAFFIC = 500;
public static final int INTERVAL_TRAFFIC = 1000;
//點播爬蟲時間
public static final int TIMEOUT_VOD = 30 * 1000;
//直播爬蟲時間

@ -6,8 +6,11 @@ import android.widget.TextView;
import com.fongmi.android.tv.App;
import java.text.DecimalFormat;
public class Traffic {
private static final DecimalFormat format = new DecimalFormat("#.0");
private static final String UNIT_KB = " KB/s";
private static final String UNIT_MB = " MB/s";
private static long lastTotalRxBytes = 0;
@ -24,14 +27,12 @@ public class Traffic {
}
private static String getSpeed() {
long total = TrafficStats.getTotalRxBytes() / 1024;
long time = System.currentTimeMillis();
long diff = (total - lastTotalRxBytes) * 1000;
long speed = diff / Math.max(time - lastTimeStamp, 1);
lastTimeStamp = time;
lastTotalRxBytes = total;
if (speed > 1000) return speed / 1000 + UNIT_MB;
else return speed + UNIT_KB;
long nowTotalRxBytes = TrafficStats.getTotalRxBytes() / 1024;
long nowTimeStamp = System.currentTimeMillis();
long speed = (nowTotalRxBytes - lastTotalRxBytes) * 1000 / Math.max(nowTimeStamp - lastTimeStamp, 1);
lastTimeStamp = nowTimeStamp;
lastTotalRxBytes = nowTotalRxBytes;
return speed < 1000 ? speed + UNIT_KB : format.format(speed / 1024f) + UNIT_MB;
}
public static void reset() {

Loading…
Cancel
Save