如何统计安卓手机的3g流量?

发布于 2024-12-02 18:08:42 字数 894 浏览 0 评论 0原文

我想做的是分别统计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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

—━☆沉默づ 2024-12-09 18:08:42

从 API 级别 8 (Android 2.2) 开始,有一个类 TrafficStats它提供了您所需要的:

提供网络流量统计的类。这些统计数据
包括发送和接收的字节以及发送的网络数据包
并通过所有接口、移动接口以及
每个 UID 基础。

在旧版本上,您可以使用您提到的方法(即读取 /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:

Class that provides network traffic statistics. These statistics
include bytes transmitted and received and network packets transmitted
and received, over all interfaces, over the mobile interface, and on a
per-UID basis.

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 between TrafficStats 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.

海夕 2024-12-09 18:08:42

要获取当前的连接类型,您可以使用 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
         }
    }

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:

    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
         }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文