[Android Pro] Android7.0 system for Android get flowmeter number Trafficstats.getuidrxbytes (UID) and trafficstats.getuidtxbytes (UID) return-1 solution

Source: Internet
Author: User

reference:http://blog.csdn.net/zhangyong7112/article/details/54574214

A recent project on the flow of the Android7.0 system to run on the phone, has been not access to traffic data, view the source and then found Trafficstats.getuidrxbytes (UID) and Trafficstats.getuidtxbytes (UID) has always been returned-1,

// gets the sum of the received and sent bytes for a network uid long total =+ trafficstats.getuidtxbytes (UID);

This is a wonder!!! Then the multi-party find information, asked the company's great God, told me that you can read the data in the "/proc/uid_stat/" folder. Then finally came out ... No, the sticker code.

if (total = = 0 | | (Trafficstats.getuidrxbytes (UID) = =-1) && (trafficstats.getuidtxbytes (UID) = =-1)    ) {=  Gettotalbytesmanual (UID);}
/*** Query the data in the folder by UID *@paramLocaluid *@return      */    PrivateLong Gettotalbytesmanual (intlocaluid) {//log.e ("bytesmanual*****", "Localuid:" + localuid);File dir =NewFile ("/proc/uid_stat/"); String[] Children=dir.list (); StringBuffer StringBuffer=NewStringBuffer ();  for(inti = 0; i < children.length; i++) {stringbuffer.append (children[i]); Stringbuffer.append ("   "); }//log.e ("children*****", Children.length + "");//log.e ("children22*****", stringbuffer.tostring ());        if(!arrays.aslist (Children). Contains (string.valueof (LOCALUID))) {            return0L; } File Uidfiledir=NewFile ("/proc/uid_stat/" +string.valueof (Localuid)); File uidactualfilereceived=NewFile (Uidfiledir, "TCP_RCV"); File uidactualfilesent=NewFile (Uidfiledir, "tcp_snd"); String textreceived= "0"; String textsent= "0"; Try{BufferedReader brreceived=NewBufferedReader (NewFileReader (uidactualfilereceived)); BufferedReader brsent=NewBufferedReader (NewFileReader (uidactualfilesent));            String Receivedline;            String Sentline; if((Receivedline = Brreceived.readline ())! =NULL) {textreceived=Receivedline;//log.e ("receivedline*****", "Receivedline:" + receivedline);            }            if((Sentline = Brsent.readline ())! =NULL) {textsent=Sentline;//log.e ("sentline*****", "Sentline:" + sentline);            }        } Catch(IOException e) {e.printstacktrace ();//log.e ("ioexception*****", e.tostring ());        }//log.e ("bytesmanualend*****", "Localuid:" + localuid);        returnLong.valueof (textreceived). Longvalue () +long.valueof (textsent). Longvalue (); }

[Android Pro] Android7.0 system for Android get flowmeter number Trafficstats.getuidrxbytes (UID) and trafficstats.getuidtxbytes (UID) return-1 solution

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.