【问题标题】:getting network/data usage via adb通过 adb 获取网络/数据使用情况
【发布时间】:2013-07-15 12:19:08
【问题描述】:

我正在寻找通过 adb/adb shell 在运行 2.3 的 android 手机上发送和接收应用程序的数据量

我找到的最近的线程是这个

Tracking an application's network statistics (netstats) using ADB

但它对我不起作用。

理想情况下,我希望查看给定应用程序的网络统计信息,并且还知道如何在需要时擦除/重置这些统计信息。

有什么想法吗?

【问题讨论】:

标签: android networking adb


【解决方案1】:

如果不需要使用 adb/adb shell,您可以使用 trafficStats 类计算 不同应用程序的互联网使用情况 安装在设备中不是必要的要求

       final PackageManager pm = getPackageManager();
        // get a list of installed apps.
        List<ApplicationInfo> packages = pm
                .getInstalledApplications(PackageManager.GET_META_DATA);

        // loop through the list of installed packages and see if the selected
        // app is in the list
        for (ApplicationInfo packageInfo : packages) {

        // get the UID for the selected app
        UID = packageInfo.uid;
          //internet usage for particular app(sent and received) 
        long recived = TrafficStats.getUidRxBytes(UID);
        long send = TrafficStats.getUidTxBytes(UID);


        }

仅适用于您的应用程序的互联网使用情况:

receivedMbs = (double) TrafficStats.getUidRxBytes(android.os.Process
                .myUid()) / (1024 * 1024);
sentMbs = (double) TrafficStats.getUidTxBytes(android.os.Process
                .myUid()) / (1024 * 1024);

相关链接:

TrafficStats Api android and calculation of daily data usage

traffic stats example

希望对你有帮助。

【讨论】:

    【解决方案2】:
    echo $(($(adb shell ifconfig rmnet_mhi0|grep "RX bytes"|cut -d ":" -f 2|cut -d " " -f 1)/1024)) Mb
    

    但这是自上次重启或上次连接以来。

    或:https://gist.github.com/Zibri/387f0bd0acf09f71384ce78dd45aa058

    #!/bin/bash
    # Get android network usage statistics from phone.
    # by Zibri
    function getUsage () 
    { 
        rb=0;
        tb=0;
        for a in $(adb shell dumpsys netstats|grep "rb="|cut -d "=" -f 3|cut -d " " -f 1);
        do
            rb=$((rb+a/1024));
        done;
        rb=$((rb/2));
        for a in $(adb shell dumpsys netstats|grep "rb="|cut -d "=" -f 5|cut -d " " -f 1);
        do
            tb=$((tb+a/1024));
        done;
        tb=$((tb/2));
        echo RX: $rb Kb TX: $tb Kb
        echo Total: $(((rb+tb)/1024)) Mb
    };
    getUsage
    

    它为您提供与设置/数据使用菜单中相同的数字。
    (就我而言,这就是我所需要的)。

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      相关资源
      最近更新 更多