如何统计安卓手机的3g流量?
我想做的是分别统计3G流量和WiFi流量。现在我知道如何使用 WiFi 了。下面是WiFi的源代码。通过这种方式,我可以统计所有制造商的所有 Android 手机的 WiFi 流量。但我还没有找到3g类似的方法。有谁知道吗?
//to get wifi interface
private static String getProp(String prop){
String output = "";
try{
Class<?> sp = Class.forName("android.os.SystemProperites");
Method get = sp.getMethod("get",String.class);
output = (String)get.invoke(null,prop);
}catch(Exception e){
e.printStackTrace();
}
return output;
}
//to get the traffic from system file
...
...
if (connectinTpe == ConnectivityManager.TYPE_WIFI){
String wifiInterface = getProp("wifi.interface");
if(wifiInterface == null || "".equals(wifiInterface)) wifiInterface = "eth0";
rxFile = "/sys/class/net/" +wifiInterface+ "/statistics/rx_bytes";
txFile = "/sys/class/net/" +wifiInterface+ "/statistics/tx_bytes";
}
...
...
What I want to do is to count 3G traffic and WiFi traffic respectively. Now I know how to do with WiFi. Below is the source code for WiFi. By this way I can count WiFi traffic for all the android phones of all manufactures. But I haven't found a similar way for 3g. Does anyone know?
//to get wifi interface
private static String getProp(String prop){
String output = "";
try{
Class<?> sp = Class.forName("android.os.SystemProperites");
Method get = sp.getMethod("get",String.class);
output = (String)get.invoke(null,prop);
}catch(Exception e){
e.printStackTrace();
}
return output;
}
//to get the traffic from system file
...
...
if (connectinTpe == ConnectivityManager.TYPE_WIFI){
String wifiInterface = getProp("wifi.interface");
if(wifiInterface == null || "".equals(wifiInterface)) wifiInterface = "eth0";
rxFile = "/sys/class/net/" +wifiInterface+ "/statistics/rx_bytes";
txFile = "/sys/class/net/" +wifiInterface+ "/statistics/tx_bytes";
}
...
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 API 级别 8 (Android 2.2) 开始,有一个类 TrafficStats它提供了您所需要的:
在旧版本上,您可以使用您提到的方法(即读取
/sys/class/net/...
文件的文件内容)。 这篇博文包含一个优秀的TrafficStats
方法和文件位置之间的映射。 这篇文章包含其作者用来读取这些文件值的源。根据它,您应该首先尝试从“/sys/class/net/rmnet0/statistics/rx_bytes
”文件读取数字(对于“接收字节”值),如果失败尝试“/sys/class/net/ppp0/statistics/rx_bytes
””。Starting from API level 8 (Android 2.2) there is a class TrafficStats which provides what you need:
On the older versions you can use the approach you mentioned (i.e. reading file content of
/sys/class/net/...
files). This blog post contains an excellent mapping betweenTrafficStats
methods and file locations. And this SO post contains the source its author used to read those files values. According to it you should first try to read number from "/sys/class/net/rmnet0/statistics/rx_bytes
" file (for "received bytes" value) and if it fails try "/sys/class/net/ppp0/statistics/rx_bytes
" instead.要获取当前的连接类型,您可以使用 TelephonyManager:http://developer. android.com/reference/android/telephony/TelephonyManager.html
首先检查设备是否连接到默认的移动数据连接,然后检查连接类型:
to get the current type of connection you can use the TelephonyManager: http://developer.android.com/reference/android/telephony/TelephonyManager.html
first check if the device is connected to the default mobile data connection and then check the connection type: