Use of the trafficstats class in Android traffic statistics

Source: Internet
Author: User
Tags unsupported
For Android traffic statistics, you can easily obtain the trafficstats class added to Version 2.2. In fact, the trafficstats class also reads the text of the file object system type provided by Linux for parsing. In the android.net. trafficstats class, multiple static methods are provided, which can be directly called for retrieval. The return type is long. If the return value is-1, the current device does not support statistics. Java Code Static long getmobilerxbytes () // gets the total number of bytes received through mobile connections, excluding WiFi static long getmobilerxpackets () // gets the total number of packets received by mobile connections. Static long getmobiletxbytes () // total number of bytes sent by mobile static long getmobiletxpackets () // total number of data packets sent by mobile static long gettotalrxbytes () // obtain the total number of accepted bytes, including static long gettotalrxpackets () such as mobile and WiFi // The total number of accepted data packets, including static long gettotaltxbytes () such as mobile and WiFi // The total number of sent bytes, total number of data packets sent, including static long gettotaltxpackets () such as mobile and WiFi, and static long getuidrxbytes (int uid) such as mobile and WiFi) // obtain the number of bytes accepted by a network uid static long getuidtxbytes (int uid) // obtain the total number of bytes sent by a network UID to receive traffic trafficstats. gettotalrxbytes (), total sending traffic trafficstats. gettotaltxbytes (); does not contain WiFi mobile phone GPRS reception trafficstats. getcyclerxbytes (); the number of GPRS sent by a mobile phone that does not contain WiFi is trafficstats. getmobiletxbytes (); the total number of received trafficstats of a process. getuidrxbytes (UID); the total sending volume of a process trafficstats. getuidtxbytes (UID); these are all started from the first time Program The statistic of the last startup. This is not the case Article "Statistics from this boot to this shutdown "! For example, note that the unit here is "kb" Java code public long gettotalrxbytes () {// get the total number of accepted bytes, including return trafficstats such as mobile and WiFi. gettotalrxbytes () = trafficstats. unsupported? 0 :( trafficstats. gettotalrxbytes ()/1024);} public long gettotaltxbytes () {// total number of sent bytes, including return trafficstats such as mobile and WiFi. gettotaltxbytes () = trafficstats. unsupported? 0 :( trafficstats. gettotaltxbytes ()/1024);} public long getmobilerxbytes () {// gets the total number of bytes received through mobile connections, excluding WiFi return trafficstats. getcyclerxbytes () = trafficstats. unsupported? 0 :( trafficstats. getmobilerxbytes ()/1024 );}
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.