diff --git a/common/src/main/java/com/yunbao/common/utils/DeviceUtils.java b/common/src/main/java/com/yunbao/common/utils/DeviceUtils.java index 40f76b35d..b80cfd3cb 100644 --- a/common/src/main/java/com/yunbao/common/utils/DeviceUtils.java +++ b/common/src/main/java/com/yunbao/common/utils/DeviceUtils.java @@ -6,10 +6,15 @@ import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Point; +import android.net.TrafficStats; import android.text.format.Formatter; import android.util.DisplayMetrics; import android.view.WindowManager; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.InputStreamReader; + /** * 设备通用类 */ @@ -97,10 +102,38 @@ public class DeviceUtils { return mi.lowMemory; } - public static String getMemory(Context context) { + /** + * 获取可用内存 格式化 + */ + public static String getMemoryToFormat(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); am.getMemoryInfo(mi); return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化 } + + /** + * 获取可用内存 + */ + public static String getMemory(Context context) { + ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); + am.getMemoryInfo(mi); + return mi.availMem + ""; + } + + private static long lastTotalRxBytes = 0; + private static long lastTimeStamp = 0; + + /** + * 获取上传网速 + */ + public static long getNetSpeed(Context context) { + long nowTotalRxBytes = TrafficStats.getUidTxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalTxBytes() / 1024); + long nowTimeStamp = System.currentTimeMillis(); + long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒转换 + lastTimeStamp = nowTimeStamp; + lastTotalRxBytes = nowTotalRxBytes; + return speed; + } } \ No newline at end of file