In Linux, all information exists in the form of files, so the traffic information of applications will also be stored in the files of the operating system. The traffic information of systems earlier than Android 2.2 is stored in the proc/net/dev (or proc/self/net/dev) file, read the file and parse it. To read the traffic of an application, read the proc/uid_stat/uid/tcp_rcv file for parsing (Note: This directory does not exist in the simulator ). To view the traffic information of an application, run the following command:
Copy codeThe Code is as follows: adb devices lists all devices
Adb-s device name shell enter the corresponding device
Cd proc: Enter the property directory of the device
Cd uid_stat enters the user id status directory. During installation, the system assigns a uid to each application.
Ls lists the user id directories of all applications under the uid_stat directory.
Enter the uid directory of the corresponding application for the cd uid.
Ls: Check the tcp_rcv and tcp_snd directories under the uid directory.
Cat tcp_rcv: view the data received by the application
Cat tcp_snd: view the data sent by the application
Note that the uid naming rules for applications written in different languages are different. For Java-based applications, the uid starts from 10000, if it is written in C, its uid starts from 1000. In addition, once the uid is assigned, it will not change. We can see thatApp_0, app_1, app_3, and app_4This information also represents the uid, that is, app_0 is equivalent to 10000, app_1 is equivalent to 10001, in fact, the information after "app _" represents the Installation Number of the application, indicates the number of applications installed on the system.
In Android 2.2, the TrafficStats class is added to implement traffic statistics. In fact, the TrafficStats class itself relies on reading and parsing the file that stores traffic information in Linux. Android.net. the TrafficStats class provides a variety of static methods that can be obtained by calling directly. The return type is long. If the returned value is-1, it indicates UNSUPPORTED, indicating that the current device does not support statistics, it should be noted that the data obtained through TrafficStats will be cleared when the phone is restarted. Therefore, if you want to continuously collect statistics on traffic, you need to save the data to the database, when the phone restarts, read the data and accumulate it. The static method of the TrafficStats class is as follows:Copy codeThe Code is as follows: package cn. sunzn. trafficmanger;
Import android. app. Activity;
Import android.net. TrafficStats;
Import android. OS. Bundle;
Import android. view. Menu;
Public class MainActivity extends Activity {
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
/** Get the total byte traffic received by the mobile phone through 2G/3G */
TrafficStats. getpolicerxbytes ();
/** Obtain the total number of data packets received by the mobile phone through 2G/3G */
TrafficStats. getMobileRxPackets ();
/** Get the total byte traffic sent by the mobile phone through 2G/3G */
TrafficStats. getMobileTxBytes ();
/** Obtain the total number of data packets sent by the mobile phone through 2G/3G */
TrafficStats. getMobileTxPackets ();
/** Get the total byte traffic received by the mobile phone in all network modes (including wifi )*/
TrafficStats. getTotalRxBytes ();
/** Obtain the total number of packets received by the mobile phone in all network modes (including wifi )*/
TrafficStats. getTotalRxPackets ();
/** Get the total byte traffic sent by the mobile phone in all network modes (including wifi )*/
TrafficStats. getTotalTxBytes ();
/** Obtain the total number of data packets sent by the mobile phone over all networks (including wifi )*/
TrafficStats. getTotalTxPackets ();
/** Obtain the total byte traffic (including wifi) received by the app corresponding to the specified UID on the mobile phone in all network modes )*/
TrafficStats. getUidRxBytes (uid );
/** Get the total byte traffic (including wifi) sent by the app corresponding to the specified UID on the mobile phone in all network modes )*/
TrafficStats. getUidTxBytes (uid );
}
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}
}