Android obtains mobile phone traffic usage

Source: Internet
Author: User

Android obtains mobile phone traffic usage

The software traffic usage data is stored in the/proc/uid_stat/uid (User ID)/file below


/Proc/uid_stat/uid/tcp_send upload traffic
/Proc/uid_stat/uid/tcp_rcv download traffic


The key code is as follows:

// 1. Obtain a package manager. PackageManager pm = getPackageManager (); // 2. retrieve the uidList appliactaionInfos = pm of all applications by traversing the mobile phone operating system. getInstalledApplications (0); for (ApplicationInfo applicationInfo: appliactaionInfos) {int uid = applicationInfo. uid; // obtain the software uid // proc/uid_stat/10086 long tx = TrafficStats. getUidTxBytes (uid); // sent upload traffic bytelong rx = TrafficStats. getUidRxBytes (uid); // The downloaded traffic byte // method return value-1 indicates that the application does not generate traffic or the operating system does not support traffic statistics} TrafficStats. getMobileTxBytes (); // get the total traffic TrafficStats uploaded on the 3g/2g mobile phone. getcyclerxbytes (); // total traffic for 2g/3g download from the mobile phone TrafficStats. getTotalTxBytes (); // all network interfaces of the mobile phone include wifi, total traffic TrafficStats for 3g and 2g uploads. getTotalRxBytes (); // all network interfaces of the mobile phone include wifi, total traffic for 3g, 2g download


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.