2

I am looking to figure out how much data an application is sending and receiving on an android handset running 2.3 through adb/adb shell

The closest thread I have found was this

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

but it doesn't work for me.

Ideally I would like to see the network stats for a given application and also know how to wipe/reset these stats if needed.

Any ideas?

Community
  • 1
  • 1
chatlow
  • 91
  • 1
  • 2
  • 5
  • If using `adb`/`adb shell` isn't a requirement, `TrafficStats` gives you the data that you need: http://developer.android.com/reference/android/net/TrafficStats.html – CommonsWare Jul 15 '13 at 12:20
  • Has to be done remotely as handset is connected to a linux machine. A bash script then runs various adb commands. Will take a look at trafficstats now.. – chatlow Jul 15 '13 at 12:33

2 Answers2

1

You can use trafficStats class to calculate internet usage for different applications installed in the device if using adb/adb shell isn't a necessary requirement

       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);


        }

Internet Usage for your application only :

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

Related links :

TrafficStats Api android and calculation of daily data usage

traffic stats example

Hope it helps .

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
1

Data usage can be found in ifconfig output for each interface. In the following example, it assumes rmnet_mhi0 is the name of the interface.

echo $(($(adb shell ifconfig rmnet_mhi0 | grep "RX bytes" | cut -d ":" -f 2|cut -d " " -f 1)/1024)) Mb

but this is since last reboot or last connection.

or: 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

which gives you the same number you get in settings/data usage menu.
(in my case this is what I needed).

Risinek
  • 390
  • 2
  • 16
Zibri
  • 9,096
  • 3
  • 52
  • 44