Android traffic monitoring trafficstats

Source: Internet
Author: User

Recently I am bored with reading about Android traffic monitoring and found that android is doing a comprehensive job in this aspect. Google has encapsulated the trafficstats for traffic monitoring and is very easy to use, for those who learn Android development, it is undoubtedly a great tool.

This statement first: most of the examples below are from the Network (that is, the example I used to find online when I was studying traffic monitoring). However, I personally think the example itself is very good, so I made some modifications and put them on shelves ~~~ Haha.

Okay, I don't want to talk much about it.

Before reading the code, let's first describe how Android calculates the traffic usage of mobile phones? In fact,

Android is a Linux-based operating system.

In Android, if you use root explorer to view system files, the specific records of these files/proc/NET/dev related to traffic monitoring are not very clear at the moment, it may be a traffic condition of the entire system.

/Proc/uid_stat/% d "and"/proc/uid_stat/% d "% d are the UID of the process. This file only contains two data items: tcp_rcv and tcp_snd.

If you look at the name, you can see what it represents. One represents the total number of accepted bytes, And the other represents the total number of sent bytes.

These two files are non-standard Linux kernel files, which are composed of the android kernel layer/kernel/NET/socket. c's _ sock_sendmsg function is responsible for writing data. User-layer socket communication will eventually call this function at the kernel layer (including local sockets and network sockets ).

Android did not encapsulate the response traffic monitoring API before 2.3. After 2.3, We encapsulated data traffic monitoring into the android.net. trafficstats class. The principle is to read the files mentioned above. Some methods are also used to read other files.

Let's take a look at the configuration file main. xml.

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <button Android: id = "@ + ID/showlist" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "show current program traffic"/> </linearlayout>

The configuration file is very simple. It only defines a button component whose ID is showlist.

Now let's take a look at the code in the activity.

Public class trafficstatsactivity extends activity {/** called when the activity is first created. */private button showbtn = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); showbtn = (button) findviewbyid (R. id. showlist); showbtn. setonclicklistener (New buttonlistener ();} public void getapptrafficlist () {// obtain all installation information on the mobile phone And obtain the permission information packagemanager PM = getpackagemanager (); // obtain the androidmanifest in each package. XML Information, its permissions, and so on list <packageinfo> pinfos = PM. getinstalledpackages (packagemanager. get_uninstalled_packages | packagemanager. get_permissions); // traverse each application package information for (packageinfo info: pinfos) {// request androidmanifest corresponding to each package. permission in XML string [] premissions = info. requestedpermissions; If (premissions! = NULL & premissions. length> 0) {// find the application for network services (string premission: premissions) {If ("android. permission. internet ". equals (premission) {// obtain the process ID of each application in the operating system int uid = info. applicationinfo. UID; // If-1 is returned, this method is not supported. Note that it must be a long RX = trafficstats of more than 2.2. getuidrxbytes (UID); // If-1 is returned, this method is not supported. Note that it must be a long Tx = trafficstats of more than 2.2. getuidtxbytes (UID); If (RX <0 | TX <0) {continue;} else {toast. maketext (this, info. applicationinfo. loadlabel (PM) + "consumed traffic --" + formatter. formatfilesize (this, RX + Tx), toast. length_short) ;}}}} private class buttonlistener implements onclicklistener {@ overridepublic void onclick (view v) {// todo auto-generated method stubgetapptrafficlist ();}}}

Note the following three codes:

// Obtain the idint uid = info of each application process in the operating system. applicationinfo. UID; // If-1 is returned, this method is not supported. Note that it must be a long RX = trafficstats of more than 2.2. getuidrxbytes (UID); // If-1 is returned, this method is not supported. Note that it must be a long Tx = trafficstats of more than 2.2. getuidtxbytes (UID );

The first code mainly serves to obtain the application process system ID with declared network permissions, which is similar to the process PID in our window.
The role of the second sentence is undoubtedly to obtain the byte (in KB) received from the network after the application is started up based on the process ID ), the last sentence is to obtain the bytes (in KB) sent to the network after the application is started based on the process ID ). Okay, nothing else. I also noted some codes in the program. Finally, the trafficstats class can only be used in Versions later than android2.2. In addition, it is best to use a real machine for testing during testing. Because the simulator may cause the RX and TX parameters to be-1 in total.
^ _ ^

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.