Analysis of the implementation principle of Android traffic monitoring _android

Source: Internet
Author: User

All the information in the Linux system is in the form of a file, so the flow information of the application is stored in the operating system files. The traffic information for the previous Android 2.2 version of the system is stored under Proc/net/dev (or Proc/self/net/dev) files, read the file, and then parse it. Reads the traffic of an application, reads the PROC/UID_STAT/UID/TCP_RCV file for resolution (note: This directory does not exist under the emulator). To view the flow information for an application, you can do so by using the following command:

Copy Code code as follows:

ADB Devices List all devices
Adb-s device name shell into the corresponding device
CD proc into the device's properties directory
The CD Uid_stat into the user ID status directory, and each application assigns a corresponding UID to each application when it is installed
LS lists the corresponding user ID directories for all applications under the Uid_stat directory
CD UID into the corresponding application's UID directory
ls to view the TCP_RCV and TCP_SND directories under the corresponding UID directory
Cat TCP_RCV View the data information that the application receives
Cat TCP_SND View the data information sent by the application

Note here that the different languages written in the corresponding UID naming rules are different, if it is a Java-written application, its UID is starting from 10000, if the C language is written, its UID is starting from 1000. In addition, the UID is not changed once it is assigned. From the image below, we can see a similar app_0, App_1, App_3, App_4Information, this information also represents the UID, that is, app_0 equivalent to 10000,app_1 equivalent to 10001, in fact, "APP_" after the information represents the application of the installation sequence number, indicating that this application is the first few are installed to the system.

Android 2.2 version of the addition of the Trafficstats class to achieve the operation of traffic statistics, in fact, the Trafficstats class itself is relying on reading Linux to save traffic information files and parse to achieve. A variety of static methods are available in the Android.net.TrafficStats class, which can be invoked directly, and the return type is long, and if the return value equals-1 for the unsupported, indicating that the current device does not support statistics, it should be noted that The data obtained through the trafficstats will be emptied when the phone restarts, so if you want to keep statistics on the traffic, you need to save the data to the database, and read the data out when the phone restarts. The static method for the Trafficstats class is as follows:

Copy Code code 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 number of bytes received by the mobile phone via 2g/3g * *
Trafficstats.getmobilerxbytes ();
/** get the total number of packets received via 2G/3G.
Trafficstats.getmobilerxpackets ();
/** gets the total number of bytes sent by the mobile phone through the 2g/3g * *
Trafficstats.getmobiletxbytes ();
/** get the total number of packets sent through the 2G/3G * *
Trafficstats.getmobiletxpackets ();
/** gets the total number of bytes of traffic received via all networks (including WiFi).
Trafficstats.gettotalrxbytes ();
/** the total number of packets (including WiFi) received by the mobile phone over all network modes.
Trafficstats.gettotalrxpackets ();
/** gets the total number of bytes of traffic sent through all networks (including WiFi) * *
Trafficstats.gettotaltxbytes ();
/** gets the total number of packets (including WiFi) sent by the mobile phone through all networks
Trafficstats.gettotaltxpackets ();
/** Get mobile phone Specifies the UID corresponding to the total number of bytes of traffic received via all networks (including WiFi) * *
Trafficstats.getuidrxbytes (UID);
/** Get cell phone specifies UID The total number of bytes of traffic sent by the application through all networks (including WiFi) * *
Trafficstats.getuidtxbytes (UID);
}
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.