For Android traffic statistics in the 2.2 version of the new Trafficstats class can be easily obtained, in fact, the Trafficstats class is also read Linux provided by the file object system type of text to parse.
Android.net.TrafficStats class, provides a variety of static methods, can be called directly to get, return type is long, if return equals-1 for unsupported current device does not support statistics.
Static long getmobilerxbytes () //Gets the total number of bytes received via the mobile connection, not including wifistatic long getmobilerxpackets () // Gets the total number of packets received by the mobile connection static long getmobiletxbytes () //mobile Total Bytes sent static long getmobiletxpackets () //mobile Total Packets sent static long gettotalrxbytes () //Gets the total number of bytes accepted, including mobile and WiFi static long Gettotalrxpackets () //Total number of accepted packets, including mobile and WiFi static long gettotaltxbytes () //Total Send bytes, Contains static long gettotaltxpackets (), such as mobile and WiFi , the total number of packets sent, including mobile and WiFi static long getuidrxbytes ( int uid) //Gets the number of bytes accepted for a network uid static long
Total accepted Traffic trafficstats.gettotalrxbytes (),
Total outgoing traffic trafficstats.gettotaltxbytes ());
Mobile Phone GPRS receiving trafficstats.getmobilerxbytes () without wifi;
Mobile phone GPRS send volume trafficstats.getmobiletxbytes () not including WiFi;
The total received trafficstats.getuidrxbytes (UID) of a process);
The total send volume trafficstats.getuidtxbytes (Uid) of a process);
These are the statistics from the first start of the program to the last start. Not the "Statistics from this boot to the shutdown" in this article! For an example of
usage, note that the units obtained here are "KB"
Public long Gettotalrxbytes () { //Gets the total number of bytes accepted, including mobile and WiFi return trafficstats.gettotalrxbytes () = = Trafficstats.unsupported?0: (Trafficstats.gettotalrxbytes ()/1024); } Public long Gettotaltxbytes () { //Total number of bytes sent, including mobile and WiFi return trafficstats.gettotaltxbytes () = = Trafficstats.unsupported?0: (Trafficstats.gettotaltxbytes ()/1024); } Public long Getmobilerxbytes () { //Gets the total number of bytes received via mobile connection, not including WiFi return trafficstats.getmobilerxbytes () = = Trafficstats.unsupported?0: (Trafficstats.getmobilerxbytes ()/1024);
Reference Link: http://blog.csdn.net/qjbagu/article/details/7102730?locationNum=11
Android_net_trafficstats.cpp source Download Link: http://download.csdn.net/detail/klcf0220/9654458
Android Traffic Statistics Trafficstats class