Files
CubeAoleyunSN/app/src/main/java/com/aoleyun/sn/utils/GetFlowUtil.java
Godfather 7a1a4fe5a5 version:2.1 MTK
fix:优化获取设备类型逻辑,优化获取设备版本号
update:打包更新
2022-04-25 18:21:25 +08:00

85 lines
3.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.aoleyun.sn.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.TrafficStats;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;
public class GetFlowUtil {
public static FlowInfo getAppFlowInfo(String pakageName, Context context) {
//获取到配置权限信息的应用程序
PackageManager pms = context.getPackageManager();
List<PackageInfo> packinfos = pms
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
//存放具有Internet权限信息的应用
FlowInfo flowInfo = new FlowInfo();
for (PackageInfo packinfo : packinfos) {
String appName = packinfo.packageName;
if (!TextUtils.isEmpty(appName)) {
if (appName.equals(pakageName)) {
//用于封装具有Internet权限的应用程序信息
//封装应用信息
flowInfo.setPackname(packinfo.packageName);
flowInfo.setIcon(packinfo.applicationInfo.loadIcon(pms));
flowInfo.setAppname(packinfo.applicationInfo.loadLabel(pms).toString());
//获取到应用的uiduser id
int uid = packinfo.applicationInfo.uid;
//TrafficStats对象通过应用的uid来获取应用的下载、上传流量信息
//发送的 上传的流量byte
flowInfo.setUpKb(TrafficStats.getUidRxBytes(uid));
//下载的流量 byte
flowInfo.setDownKb(TrafficStats.getUidTxBytes(uid));
break;
}
}
}
return flowInfo;
}
private static long[] getStat(int uid) {
String line, line2;
long[] stats = new long[2];
try {
File fileSnd = new File("/proc/uid_stat/" + uid + "/tcp_snd");
File fileRcv = new File("/proc/uid_stat/" + uid + "/tcp_rcv");
BufferedReader br1 = new BufferedReader(new FileReader(fileSnd));
BufferedReader br2 = new BufferedReader(new FileReader(fileRcv));
while ((line = br1.readLine()) != null && (line2 = br2.readLine()) != null) {
stats[0] = Long.parseLong(line);
stats[1] = Long.parseLong(line2);
}
br1.close();
br2.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("getStat: ", e.getMessage());
}
return stats;
}
//将字节数转化为MB
public static String byteToMB(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {
return String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {
float f = (float) size / mb;
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size > kb) {
float f = (float) size / kb;
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} else {
return String.format("%d B", size);
}
}
}