gpt4 book ai didi

java - 在获取应用程序数据使用时满足运行 Jelly bean 以及 Jelly bean 之后版本的设备

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:08 26 4
gpt4 key购买 nike

我想满足在 Jelly Bean 版本及以下版本以及 Jelly Bean 以上版本上运行的设备。

我的方法应该根据应用程序 ID 获取所有应用程序的应用程序使用情况/流量。请注意这行 rx = Long.parseLong(String.valueOf(id)); 在第一个 if 子句上,它适用于运行版本小于或等于 Jelly Bean 的设备。

使用 TrafficStats.getUidTxBytes(uid) 获取基于其 ID 的已安装应用程序的数据使用情况,但在 4.3 中仅返回 0 值,但是,else 子句使用 TrafficStats.getUidTxBytes(uid) 在 5 以上的版本中准确检索每个应用程序的应用程序使用情况。

我特别关注 if 子句,该子句适用于运行低于 5 的 android 版本的设备,例如在本例中为 4.3 (Jelly Bean)

public void recordSnapshot(Context context)
{
TinyDB settings = new TinyDB(context);
int boot_id = settings.getInt(AppPreferences.BOOT_ID);
PackageManager pm = context.getPackageManager();

for (ApplicationInfo app : pm.getInstalledApplications(0))
{
String androidOS = Build.VERSION.RELEASE;
int currentapiVersion = android.os.Build.VERSION.SDK_INT;

long tx = 0;
long rx = 0;
int uid = app.uid;

if(currentapiVersion <= Build.VERSION_CODES.JELLY_BEAN)
{
File dir = new File("/proc/uid_stat/");
String[] children = dir.list();
List<Integer> uids = new ArrayList<Integer>();

for (int i = 0; i < children.length; i++) {
uid = Integer.parseInt(children[i]);
String uidString = String.valueOf(uid);
File uidFileDir = new File("/proc/uid_stat/" + uidString);
File uidActualFile = new File(uidFileDir, "tcp_rcv");
StringBuilder text = new StringBuilder();

try {
BufferedReader br = new BufferedReader(new FileReader(uidActualFile));
String line;

while ((line = br.readLine()) != null) {
Log.d(String.valueOf(uid), line);//this returns the amount of data received for the particular uid
rx = Long.parseLong(String.valueOf(uid));
text.append(line);

text.append('\n');
}
} catch (IOException e) {
//handle this
}

uids.add(id);
}
}
else {
tx = TrafficStats.getUidTxBytes(uid);
rx = TrafficStats.getUidRxBytes(uid);
}
}

整个方法

public void recordSnapshot(Context context)
{
TinyDB settings = new TinyDB(context);
int boot_id = settings.getInt(AppPreferences.BOOT_ID);
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
int networkType = NetworkState.GetNetworkState(context, info, "DataUsageRecorder"); // wifi, data, data roaming

// Get all apps
PackageManager pm = context.getPackageManager();
for (ApplicationInfo app : pm.getInstalledApplications(0))
{

String androidOS = Build.VERSION.RELEASE;
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
long tx = 0;
long rx = 0;
int uid = app.uid;


if(currentapiVersion <= Build.VERSION_CODES.JELLY_BEAN_MR2)
{

File dir = new File("/proc/uid_stat/");
String[] children = dir.list();
List<Integer> uids = new ArrayList<Integer>();

for (int i = 0; i < children.length; i++) {
uid = Integer.parseInt(children[i]);
String uidString = String.valueOf(uid);
File uidFileDir = new File("/proc/uid_stat/" + uidString);
File uidActualFile = new File(uidFileDir, "tcp_rcv");
StringBuilder text = new StringBuilder();

try {
BufferedReader br = new BufferedReader(new FileReader(uidActualFile));
String line;

while ((line = br.readLine()) != null) {
Log.d(String.valueOf(uid), line);//this returns the amount of data received for the particular uid
rx = Long.parseLong(String.valueOf(uid));
//text.append(line);

//text.append('\n');
}
} catch (IOException e) {
//handle this
}

uids.add(uid);
}
}

else
{

tx = TrafficStats.getUidTxBytes(uid);
rx = TrafficStats.getUidRxBytes(uid);
}




if ((tx == 0 || rx == 0))
{
// Skip inactive items
continue;
}
else if (Globals.DEBUG && (tx < DEBUG_5MB && rx < DEBUG_5MB)) {
// Let's skip all the BS for quick testing
continue;
}


// Get package name
String package_name;
try {
CharSequence name = pm.getApplicationLabel(app);
package_name = name != null ? name.toString() : "";
} catch (Exception e) {
e.printStackTrace();
package_name = "";
}

AppUsage totals;
AppUsage appUsage;

// Get current data entry for app
//appUsage = appUsageDao.queryBuilder().where(AppUsageDao.Properties.App_uid.eq(uid), AppUsageDao.Properties.Type.eq(networkType), AppUsageDao.Properties.Boot_id.eq(boot_id)).limit(1).unique();

// Get last recorded totals since device boot
totals = appUsageDao.queryBuilder().where(AppUsageDao.Properties.App_uid.eq(uid), AppUsageDao.Properties.Type.eq(NetworkState.ALL), AppUsageDao.Properties.Boot_id.eq(boot_id)).limit(1).unique();

long tx_diff = tx;
long rx_diff = rx;

if (totals != null)
{
// Get difference, and update
tx_diff -= totals.getTx();
rx_diff -= totals.getRx();

totals.setTx(tx);
totals.setRx(rx);
}
else
{
// add new master
totals = new AppUsage(null, new Date(), uid, package_name, NetworkState.ALL, tx_diff, rx_diff, 0, 0, boot_id);
}

// add new app
appUsage = new AppUsage(null, new Date(), uid, package_name, networkType, tx_diff, rx_diff, 0, 0, boot_id);


/*if (appUsage == null)
{
// Create new
appUsage = new AppUsage(null, new Date(), uid, package_name, networkType, tx, rx, 0, 0, boot_id);
}
else
{
// Update
appUsage.setTx(tx);
appUsage.setRx(rx);
}*/

try {
// master
appUsageDao.insertOrReplace(totals);
} catch (DaoException e) {
e.printStackTrace();
}

try {
appUsageDao.insertOrReplace(appUsage);
} catch (DaoException e) {
e.printStackTrace();
}

//apps.put(app.uid, new DataUsageItem(app.uid, app.packageName, pm.getApplicationLabel(app).toString()));
}
}

最佳答案

尽管文档说流量统计在 4.3 上似乎不能很好地工作,因为在某些情况下它适用于某些应用程序 ID 而不是某些应用程序 ID,因此我会绕过整个流量统计类并创建两个指向的自定义方法包含应用程序使用数据的 native c 文件,用于在同一类中检索 tx(传输数据)和 Rx(接收数据)

   long tx = yourClass.getUidTxBytes(uid);
long rx = yourClass.getUidRxBytes(uid);

然后用于 RX

public static Long getUidRxBytes(int uid) {
BufferedReader reader;
Long rxBytes = 0L;
try {
reader = new BufferedReader(new FileReader("/proc/uid_stat/" + uid
+ "/tcp_rcv"));
rxBytes = Long.parseLong(reader.readLine());
reader.close();
}
catch (FileNotFoundException e) {
rxBytes = TrafficStats.getUidRxBytes(uid);
//e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return rxBytes;
}**

然后为 TX

 public static Long getUidTxBytes(int uid) {
BufferedReader reader;
Long txBytes = 0L;
try {
reader = new BufferedReader(new FileReader("/proc/uid_stat/" + uid
+ "/tcp_snd"));
txBytes = Long.parseLong(reader.readLine());
reader.close();
}
catch (FileNotFoundException e) {
txBytes = TrafficStats.getUidTxBytes(uid);
//e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return txBytes;
}

关于java - 在获取应用程序数据使用时满足运行 Jelly bean 以及 Jelly bean 之后版本的设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37181492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com