最近做个需求要在native层的某个应用中加入流量统计,所以研究了一下Android自带的流量统计代码,将JNI中的代码直接移植到了我需要的地方
Android提供的流量统计功能在TrafficStats.java类中,包含以下几个:
- public static long getMobileTxPackets() //Return number of packets transmitted across mobile networks since device boot
- public static long getMobileRxPackets() //Return number of packets received across mobile networks since device boot
- public static long getMobileTxBytes() //Return number of bytes transmitted across mobile networks since device boot
- public static long getMobileRxBytes() //Return number of bytes received across mobile networks since device boot
- public static long getMobileTcpRxPackets() //tcp received
- public static long getMobileTcpTxPackets() //tcp transmitted
- public static long getTotalTxPackets //Return number of packets transmitted since device boot
- public static long getTotalRxPackets() //Return number of packets received since device boot
- public static long getTotalTxBytes() //Return number of bytes transmitted since device boot
- public static long getTotalRxBytes()

本文探讨了如何在Android中实现流量统计,主要关注TrafficStats类中的方法,如getMobileTxPackets、getMobileRxPackets等。文章详细解释了getTotalTxBytes方法的工作原理,涉及BPF(伯克利包过滤器)和系统文件的交互,以及如何通过bpfGetIfaceStats和parseIfaceStats获取流量数据。
最低0.47元/天 解锁文章
1989

被折叠的 条评论
为什么被折叠?



