我想做的是分别统计3G流量和WiFi流量。现在我知道如何处理WiFi了。下面是WiFi的源代码。通过这种方式,我可以统计所有制造商的所有安卓手机的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";
}
...
...
发布于 2011-09-02 20:59:14
从API8级(Android2.2)开始,有一个类TrafficStats,它提供了您需要的东西:
提供网络流量统计信息的
类。这些统计信息包括所有接口、移动接口和基于每个UID的传输和接收的字节数以及网络数据包的传输和接收。
在旧版本中,您可以使用您提到的方法(即读取/sys/class/net/...
文件的文件内容)。This blog post在TrafficStats
方法和文件位置之间包含了一个很好的映射。this SO post包含作者用来读取这些文件值的源。根据它,你应该首先尝试从"/sys/class/net/rmnet0/statistics/rx_bytes
“文件中读取数字(对于"received bytes”值),如果读取失败,请尝试"/sys/class/net/ppp0/statistics/rx_bytes
“。
发布于 2011-09-02 17:03:22
要获取当前的连接类型,可以使用TelephonyManager:http://developer.android.com/reference/android/telephony/TelephonyManager.html
首先检查设备是否连接到默认的移动数据连接,然后检查连接类型:
if (connectinTpe == ConnectivityManager.TYPE_MOBILE)
{
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int curConnectionType = tm.getNetworkType();
if(curConnectionType >= /*connection type you are looking for*/)
{
// do what you want
}
}
https://stackoverflow.com/questions/7281076
复制相似问题