Http://keepcleargas.bitbucket.org/2013/10/12/android-App-Traffic.html
Android App Traffic statistics
OCT 2013
About Android Traffic
- Traffic Statistics file : path
/proc/net/dev
such as: Lo for local traffic, rmnet0 for 3g/2g traffic, Wlan0 for wireless traffic.
-
/sys/class/net/you can find directories under related categories, such as rmnet0. In its subdirectories statistics downstream RXbytes and txbytes record incoming and outgoing traffic.
-
In /proc/uid_stat/{uid}/tcp_rcv recording the uid app download traffic bytes, /proc/uid_stat/{uid}/tcp_snd there is the Uid app upload traffic bytes
Trafficstats Learning
- Trafficstats Google Develop documentation
- Trafficstats source File View
- Important API:
StaticLongGetmobilerxbytes()Gets the total number of bytes received via the mobile connection, not including WiFiStaticLongGetmobilerxpackets()Get the total number of packets received by the mobile connectionStaticLongGetmobiletxbytes()Total number of bytes sent by mobileStaticLongGetmobiletxpackets()Total number of packets sent by mobileStaticLongGettotalrxbytes()Gets the total number of accepted bytes, including mobile and WiFi, etc.Staticlong gettotalrxpackets () //total number of accepted packets, Includes mobile and WiFi static long gettotaltxbytes () Span class= "C1" >//total number of bytes sent, including mobile and WiFi static long () //the total number of packets sent, including mobile and WiFi static long getuidrxbytes (int uid< Span class= "O" >) //get the number of bytes accepted for a network uid static long (int uid) // Gets the number of bytes sent for a network uid
Note: The Trafficstats class appears after the Android 2.2 API level (8).
Design a reasonable app traffic statistic
- Recording the use of an app's switching machine traffic
Because getUidRxBytes(int uid) and getUidTxbytes(int uid) logged is the UID app when the phone starts up to the traffic statistics that you use when using this method. It is necessary to record your use of this program in the process of the switching machine.
public class ShutDownReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //在关机时 记录uid应用 本次流量结余 } }
- Record traffic usage in Wi-Fi
Because getUidRxBytes(int uid) and getUidTxbytes(int uid) includes all network forms of traffic, including WiFi and 3g/2g. It is necessary to monitor the WiFi changes and record the traffic records used by the UID app during the WiFi process.
PublicClassWifistatereceiverExtendsBroadcastreceiverImplementsIsusoconstants{@OverridePublicvoidOnReceive(ContextContext,IntentIntent){If(Intent.Getaction().equals(Wifimanager.Wifi_state_changed_action)){IntWifistate=Intent.Getintextra(Wifimanager.extra_wifi_statewifimanager.if (wifistate == wifimanagerwifi_state_disabled) {//if } else if (== wifimanager. Wifi_state_enabled) {//record traffic for the current UID application. } } }} /span>
- Use daily alarm statistics/power-up to determine if the statistics were completed yesterday.
The daily Balance calculates the total traffic for that day and the usage of WiFi traffic and 2g/3g traffic.
PackagemanagerPm=Context.Getpackagemanager();ApplicationInfoai = pm. Getapplicationinfo("com.51zaozao.traffic", packagemanager. Get_activities); long intraffic = trafficstats. Getuidrxbytes(ai. UID); long outtraffic = trafficstats. Getuidtxbytes(ai. UID);
Expand Reading
Android app traffic statistics