我需要检查我的Android应用程序的网络使用情况。在我的应用程序中,我调用了许多Web服务API。
我想知道我的应用程序在完全运行时消耗的互联网流量是多少(以kB/MB为单位)。
我该如何检查呢?有什么工具可以用来检查吗?
我需要检查我的Android应用程序的网络使用情况。在我的应用程序中,我调用了许多Web服务API。
我想知道我的应用程序在完全运行时消耗的互联网流量是多少(以kB/MB为单位)。
我该如何检查呢?有什么工具可以用来检查吗?
为了查看目的,您可以按照MD提到的方式在监视器中检查它。
为了存储,您可以以编程方式实现。
int UID = android.os.Process.myUid();
rxBytes += getUidRxBytes(UID);
txBytes += getUidTxBytes(UID);
/**
* Read UID Rx Bytes
*
* @param uid
* @return rxBytes
*/
public 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;
}
/**
* Read UID Tx Bytes
*
* @param uid
* @return txBytes
*/
public 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;
}
}