Statistics for Android traffic

Source: Internet
Author: User

1 statistics on the traffic of Android architecture through a Trafficstats class can be directly obtained
Get total accepted Traffic trafficstats.gettotalrxbytes (),
Get total Send Traffic trafficstats.gettotaltxbytes ());
Get GPRS receive trafficstats.getmobilerxbytes ()) for mobile phones that do not include WiFi;
Get GPRS Send volume trafficstats.getmobiletxbytes () for mobile phones that do not include WiFi;

Statistics the total receive volume of a process trafficstats.getuidrxbytes (Uid));
Statistics the total sending volume of a process trafficstats.getuidtxbytes (Uid));

These acquired traffic is counted from one boot to the time of reading.
Therefore, the statistics of a program of traffic statistics, it is important to pay attention to the switch machine, and this boot is the first time after the start of the program.

2 Android Trafficstats Class
The data inside the first four read/proc/net/dev
The following two interfaces to a process of traffic statistics is the/proc/uid_stat/*** interface inside the node data

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 that the phone receives through 2G/3G */
Trafficstats.getmobilerxbytes ();
/** get the total number of packets received by the phone via 2g/3g */
Trafficstats.getmobilerxpackets ();
/** gets the total number of bytes of traffic sent through 2G/3G */
Trafficstats.getmobiletxbytes ();
/** get the total number of packets sent by the phone via 2g/3g */
Trafficstats.getmobiletxpackets ();
/** get the total number of bytes of traffic received by the phone over all networks (including WiFi) */
Trafficstats.gettotalrxbytes ();
/** get the total number of packets received by the phone over all networks (including WiFi) */
Trafficstats.gettotalrxpackets ();
/** get the total number of bytes of traffic that the phone sends over all networks (including WiFi) */
Trafficstats.gettotaltxbytes ();
/** get the total number of packets that the phone sends over all networks (including WiFi) */
Trafficstats.gettotaltxpackets ();
/** gets the total number of bytes traffic (including WiFi) that is received by the program with the specified UID for the phone, using all network methods
Trafficstats.getuidrxbytes (UID);
/** gets the total number of bytes traffic (including WiFi) sent by the application that corresponds to the specified UID for the phone over all networks
Trafficstats.getuidtxbytes (UID);
}
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}

Several applications in Android OS are collective, including (Android system, settings store, settings, System user interface, MIUI)

Os in the various modules of the traffic statistics are calculated to the OS 1000 traffic, if a module is not able to pull out problems, you can create an interface to calculate the respective.

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.