[Android Pro] Android7.0系統 關於Android擷取流量計數TrafficStats.getUidRxBytes(uid)和TrafficStats.getUidTxBytes(uid)返回-1解決方案

來源:互聯網
上載者:User

標籤:byte   接收   項目   log   讀取   取資料   讀取資料   color   file   

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

最近一個關於流量的項目在Android7.0系統的手機上運行,一直擷取不到流量的使用資料,查看源碼然後發現TrafficStats.getUidRxBytes(uid)和TrafficStats.getUidTxBytes(uid)一直都是返回的-1,

// 擷取某個網路UID接收和發送位元組的總和long total = TrafficStats.getUidRxBytes(uid) + TrafficStats.getUidTxBytes(uid);

這就納悶了!!!然後多方尋找資料,問了公司的大神,給我說可以在“/proc/uid_stat/"檔案夾中讀取資料。後來總算是出來了。。。 不說了 貼代碼

if (total == 0 || (TrafficStats.getUidRxBytes(uid) == -1) && (TrafficStats.getUidTxBytes(uid) == -1)) {    total = getTotalBytesManual(uid);}
/**     * 通過uid查詢檔案夾中的資料     * @param localUid     * @return      */    private Long getTotalBytesManual(int localUid) {//        Log.e("BytesManual*****", "localUid:" + localUid);        File dir = new File("/proc/uid_stat/");        String[] children = dir.list();        StringBuffer stringBuffer = new StringBuffer();        for (int i = 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))) {            return 0L;        }        File uidFileDir = new File("/proc/uid_stat/" + String.valueOf(localUid));        File uidActualFileReceived = new File(uidFileDir, "tcp_rcv");        File uidActualFileSent = new File(uidFileDir, "tcp_snd");        String textReceived = "0";        String textSent = "0";        try {            BufferedReader brReceived = new BufferedReader(new FileReader(uidActualFileReceived));            BufferedReader brSent = new BufferedReader(new FileReader(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);        return Long.valueOf(textReceived).longValue() + Long.valueOf(textSent).longValue();    }

 

[Android Pro] Android7.0系統 關於Android擷取流量計數TrafficStats.getUidRxBytes(uid)和TrafficStats.getUidTxBytes(uid)返回-1解決方案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.