当我们想知道应用联网所需要的的流量时,一般会用到trafficstats类的方法。
trafficstats类有以下公共方法:
static void |
clearThreadStatsTag()
Clear any active tag set to account
Socket traffic originating from the current thread.
|
static long |
getMobileRxBytes()
Return number of bytes received across mobile networks since device boot.
|
static long |
getMobileRxPackets()
Return number of packets received across mobile networks since device boot.
|
static long |
getMobileTxBytes()
Return number of bytes transmitted across mobile networks since device boot.
|
static long |
getMobileTxPackets()
Return number of packets transmitted across mobile networks since device boot.
|
static int |
getThreadStatsTag()
Get the active tag used when accounting
Socket traffic originating from the current thread.
|
static long |
getTotalRxBytes()
Return number of bytes received since device boot.
|
static long |
getTotalRxPackets()
Return number of packets received since device boot.
|
static long |
getTotalTxBytes()
Return number of bytes transmitted since device boot.
|
static long |
getTotalTxPackets()
Return number of packets transmitted since device boot.
|
static long |
getUidRxBytes(int uid)
Return number of bytes received by the given UID since device boot.
|
static long |
getUidRxPackets(int uid)
Return number of packets received by the given UID since device boot.
|
static long |
getUidTcpRxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2 , transport layer statistics are no longer available, and will always returnUNSUPPORTED .
|
static long |
getUidTcpRxSegments(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2 , transport layer statistics are no longer available, and will always returnUNSUPPORTED .
|
static long |
getUidTcpTxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2 , transport layer statistics are no longer available, and will always returnUNSUPPORTED .
|
static long |
getUidTcpTxSegments(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2 , transport layer statistics are no longer available, and will always returnUNSUPPORTED .
|
static long |
getUidTxBytes(int uid)
Return number of bytes transmitted by the given UID since device boot.
|
static long |
getUidTxPackets(int uid)
Return number of packets transmitted by the given UID since device boot.
|
static long |
getUidUdpRxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2 , transport layer statistics are no longer available, and will always returnUNSUPPORTED .
|
static long |
getUidUdpRxPackets(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2 , transport layer statistics are no longer available, and will always returnUNSUPPORTED .
|
static long |
getUidUdpTxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2 , transport layer statistics are no longer available, and will always returnUNSUPPORTED .
|
static long |
getUidUdpTxPackets(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2 , transport layer statistics are no longer available, and will always returnUNSUPPORTED .
|
static void |
incrementOperationCount(int tag, int operationCount)
Increment count of network operations performed under the given accounting tag.
|
static void |
incrementOperationCount(int operationCount)
Increment count of network operations performed under the accounting tag currently active on the calling thread.
|
static void |
setThreadStatsTag(int tag)
Set active tag to use when accounting
Socket traffic originating from the current thread.
|
static void |
tagSocket(
Socket socket)
Tag the given
Socket with any statistics parameters active for the current thread.
|
static void |
untagSocket(
Socket socket)
Remove any statistics parameters from the given
Socket .
|
static long getMobileRxBytes() //获取通过Mobile连接收到的字节总数,不包含WiFi
static long getMobileRxPackets() //获取Mobile连接收到的数据包总数
static long getMobileTxBytes() //Mobile发送的总字节数
static long getMobileTxPackets() //Mobile发送的总数据包数
static long getTotalRxBytes() //获取总的接受字节数,包含Mobile和WiFi等
static long getTotalRxPackets() //总的接受数据包数,包含Mobile和WiFi等
static long getTotalTxBytes() //总的发送字节数,包含Mobile和WiFi等
static long getTotalTxPackets() //发送的总数据包数,包含Mobile和WiFi等
static long getUidRxBytes(int uid) //获取某个网络UID的接受字节数
static long getUidTxBytes(int uid) //获取某个网络UID的发送字节数
总接受流量TrafficStats.getTotalRxBytes(),
总发送流量TrafficStats.getTotalTxBytes())
但是Android4.2.2及Android4.3却获取不到
先看一个例子
PackageManager manager = mContext.getPackageManager();
ArrayList<PackageInfo> appInfoList = getAllApps();
for (PackageInfo info : appInfoList) {
ApplicationInfo applicationInfo = info.applicationInfo;
String pkg = info.packageName;
String app = manager.getApplicationLabel(applicationInfo)
.toString();
int uid = info.applicationInfo.uid;
long rx = TrafficStats.getUidRxBytes(uid);
long tx = TrafficStats.getUidTxBytes(uid);
Log.i("tag",
"-----------------------uid=" + uid + "--rx=" + rx
+ "--tx=" + tx + "---rx1="
+ TrafficStats.getTotalRxBytes()/1024 + "---tx1="
+ TrafficStats.getTotalTxBytes()/1024);
}
public ArrayList<PackageInfo> getAllApps() {
ArrayList<PackageInfo> appList = new ArrayList<PackageInfo>();
PackageManager pm = mContext.getPackageManager();
List<PackageInfo> appList_temp = pm.getInstalledPackages(0);
for (int i = 0; i < appList_temp.size(); i++) {
PackageInfo pak = appList_temp.get(i);
// 过滤系统的程序
// if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <=
// 0) {
// appList.add(pak);
// }
if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
appList.add(pak);
}
}
appList_temp.clear();
return appList;
}
在Android4.3上面,是0.
但是我们可以用以下方法获取
private Long getTotalBytesManual(int localUid){
File dir = new File("/proc/uid_stat/");
String[] children = dir.list();
if(!Arrays.asList(children).contains(String.valueOf(localUid))){
return 0L;
}
File uidFileDir = new File("/proc/uid_stat/"+String.valueOf(localUid));
File uidActualFileReceived = new File(uidFileDir,"tcp_rcv");
File uidActualFileSent = new File(uidFileDir,"tcp_snd");
String textReceived = "0";
String textSent = "0";
try {
BufferedReader brReceived = new BufferedReader(new FileReader(uidActualFileReceived));
BufferedReader brSent = new BufferedReader(new FileReader(uidActualFileSent));
String receivedLine;
String sentLine;
if ((receivedLine = brReceived.readLine()) != null) {
textReceived = receivedLine;
}
if ((sentLine = brSent.readLine()) != null) {
textSent = sentLine;
}
}
catch (IOException e) {
}
return Long.valueOf(textReceived).longValue() + Long.valueOf(textSent).longValue();
}