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