• [Android Pro] Android7.0系统 关于Android获取流量计数TrafficStats.getUidRxBytes(uid)和TrafficStats.getUidTxBytes(uid)返回-1解决方案


    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();
        }
  • 相关阅读:
    Linux下运行java项目
    Matlab 绘图完整入门
    Matlab命令合集 妈妈再也不用担心我不会用matlab了
    详尽全面的matlab绘图教程
    拉格朗日乘子法 那些年学过的高数
    【转】几款网络仿真软件的比较
    正则表达式30分钟入门教程 ——堪称网上能找到的最好的正则式入门教程
    并发编程详细整理
    高并发通信模型NIO
    Java并发编程的艺术笔记(九)——FutureTask详解
  • 原文地址:https://www.cnblogs.com/0616--ataozhijia/p/6628462.html
Copyright © 2020-2023  润新知