新增获取上传网速的方法

This commit is contained in:
zlzw 2022-12-29 17:55:41 +08:00
parent d81e6b9f04
commit 0b5598346e

View File

@ -6,10 +6,15 @@ import android.content.Context;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Point; import android.graphics.Point;
import android.net.TrafficStats;
import android.text.format.Formatter; import android.text.format.Formatter;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.WindowManager; 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; return mi.lowMemory;
} }
public static String getMemory(Context context) { /**
* 获取可用内存 格式化
*/
public static String getMemoryToFormat(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi); am.getMemoryInfo(mi);
return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化 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;
}
} }