有个API,好像是trafficstate?trafficstatus?可以统计每个UID的流量,不过要从2.3开始支持。
不过朋友,如果你没有用这个API实现的话,我求实现方法,指点一二就行。

解决方案 »

  1.   

    http://blog.csdn.net/forlong401/article/details/8440290
      

  2.   

    android2.2以后的版本提供了trafficstatus流量统计的接口,提供了获取流量统计的方法(详细可以参考相关APi),但是仅这些远不能实现我们设计的需求。trafficstatus获取流量的方法,可以获的总得发送和接收的数据,针对uid可以获取应用的总的发送和接收的数据(这里针对uid一个app不能简单的分离wifi和3g/2g的使用数据量),还有一个考虑就是这些api提供的方法仅是从手机最近一次开机到当前的流量统计,考虑到这些问题,结合我们实际当中应用到的场景,似乎要考虑的设计不是一点两点了。所以我们还需要考虑每次开关机的数据记录,wifi状态下的数据记录,还有当日流量的结余,以及当月流量的结余考虑到这些,再开始设计怎么去实现。
      

  3.   


    if (Build.VERSION.SDK_INT >= 18) {
    mobileRxBytes = getUidRxBytes(uid);
    //mobileRxPackets = TrafficStats.getUidRxPackets(uid);
    mobileRxPackets =0; // packets暂时用不到,先设置为0
    mobileTxBytes = getUidTxBytes(uid);
    //mobileTxPackets = TrafficStats.getUidTxPackets(uid);
    mobileTxPackets =0; // packets暂时用不到,先设置为0
    } else if (Build.VERSION.SDK_INT >= 12) {
    mobileRxBytes = TrafficStats.getUidRxBytes(uid);
    //mobileRxPackets = TrafficStats.getUidRxPackets(uid);
    mobileRxPackets =0; // packets暂时用不到,先设置为0
    mobileTxBytes = TrafficStats.getUidTxBytes(uid);
    //mobileTxPackets = TrafficStats.getUidTxPackets(uid);
    mobileTxPackets =0; // packets暂时用不到,先设置为0
    } else {
    mobileRxBytes = TrafficStats.getUidRxBytes(uid);
    mobileRxPackets = 0;
    mobileTxBytes = TrafficStats.getUidTxBytes(uid);
    mobileTxPackets = 0;
    }private static Long getUidRxBytes(int localUid) {
    if (children == null) {
    File dir = new File("/proc/uid_stat/");
    children = dir.list();
    }
    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");
    String textReceived = "0";
    try {
    BufferedReader brReceived = new BufferedReader(new FileReader(
    uidActualFileReceived));
    String receivedLine;
    if ((receivedLine = brReceived.readLine()) != null) {
    textReceived = receivedLine;
    }
    } catch (IOException e) {
    }
    return Long.valueOf(textReceived).longValue();
    }
      

  4.   

    这个获取到的是应用接收到的总的字节数 但是没法区分wifi使用量和3g使用量;另外这里统计的是从开机到当前的流量
      

  5.   

    楼主做好没?只要某个程序的3G流量,不要wifi流量的,做好了共享一下啊
    发邮箱[email protected]
      

  6.   

    我现在要实现 监控单个app的移动流量和wifi流量。不知道有什么解决的办法