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