WIS*_*SHY 29 api android network-monitoring
我需要查看我的Android应用程序的互联网消费情况.在我的应用程序中,我调用了大量的Web服务API.
我想知道我的应用程序在完整的时间内以KB/MB的速度消耗互联网的程度.
我该怎么检查?有没有工具来检查?
M D*_*M D 25
Android Studio 2.0引入新的Network部分Android Monitor,可以帮助您解决问题.
Tx == Transmit Bytes Rx == Receive Bytes
Run Code Online (Sandbox Code Playgroud)
出于查看目的,您可以在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;
}
}
Run Code Online (Sandbox Code Playgroud)
看看:Android Monitor.
因此,您可以监控许多主题.
在网络监视器中显示正在运行的应用程序
跟着这些步骤:
任何网络流量都开始出现在网络监视器中:
网络监视器会增加设备传输和接收千字节数据所需的时间.y轴以千字节/秒为单位.x轴以秒开始,然后是分钟和秒,依此类推.
参考:Android监视器
| 归档时间: |
|
| 查看次数: |
14915 次 |
| 最近记录: |