Use of the Android TrafficStats class, trafficstats

Source: Internet
Author: User

Use of the Android TrafficStats class, trafficstats
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

  1. Static long getMobileRxBytes () // gets the total number of bytes received through Mobile connections, excluding WiFi
  2. Static long getMobileRxPackets () // gets the total number of packets received by the Mobile connection.
  3. Static long getMobileTxBytes () // total number of bytes sent by Mobile
  4. Static long getMobileTxPackets () // total number of data packets sent by Mobile
  5. Static long getTotalRxBytes () // obtain the total number of accepted bytes, including Mobile and WiFi.
  6. Static long getTotalRxPackets () // total number of accepted packets, including Mobile and WiFi
  7. Static long getTotalTxBytes () // The total number of sent bytes, including Mobile and WiFi
  8. Static long getTotalTxPackets () // total number of data packets sent, including Mobile and WiFi
  9. Static long getUidRxBytes (int uid) // gets the number of bytes accepted by a network UID
  10. Static long getUidTxBytes (int uid) // gets the number of bytes sent by a network UID

Total accepted traffic TrafficStats. getTotalRxBytes (),
Total sending traffic TrafficStats. getTotalTxBytes ());
Mobile phone GPRS reception volume TrafficStats. getMobileRxBytes () that does not contain WIFI ());
The number of sent messages sent by the mobile phone without Wifi (TrafficStats. getMobileTxBytes ());

The total receiving volume of a process, TrafficStats. getUidRxBytes (Uid ));
Total sending volume of a process TrafficStats. getUidTxBytes (Uid ));

These are statistics from the first start of the program to the last start. This is not the "statistics from this boot to this shutdown" mentioned in this article "!

For example, note that the unit here is "KB"
Java code
  1. Public long getTotalRxBytes () {// obtain the total number of accepted bytes, including Mobile and WiFi.
  2. Return TrafficStats. getTotalRxBytes () = TrafficStats. UNSUPPORTED? 0 :( TrafficStats. getTotalRxBytes ()/1024 );
  3. }
  4. Public long getTotalTxBytes () {// total number of sent bytes, including Mobile and WiFi
  5. Return TrafficStats. getTotalTxBytes () = TrafficStats. UNSUPPORTED? 0 :( TrafficStats. getTotalTxBytes ()/1024 );
  6. }
  7. Public long getMobileRxBytes () {// gets the total number of bytes received through Mobile connections, excluding WiFi
  8. Return TrafficStats. getMobileRxBytes () = TrafficStats. UNSUPPORTED? 0 :( TrafficStats. getMobileRxBytes ()/1024 );
  9. }

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.