How can I check my Android app's network consu

2019-03-11 01:51发布

问题:

I need to check my Android app's Internet consumption. In my app I have a numerous number of web service APIs being called.

I want to know how much my app consumes the Internet in kB/MB at a full go.

How can I check that? Is there any tool to check that?

回答1:

Android Studio 2.0 Introduce new Network section in Android Monitor which can help you with your problem.

Tx == Transmit Bytes Rx == Receive Bytes


回答2:

There are three ways...

  1. You can view in Device/Emulator. Go to Setting -> Data usage, and find your application in the list

  2. In Eclipse, select DDMS (perspective) -> Select your package from Devices (left side) -> Click on Network Statistics tab -> Click Start

  3. As already answered, in Android Studio, go to Android Monitor (bottom tab) -> Network (tab) -> look for Tx (Transmit Data) / Rx (Receive Data)



回答3:

For view purposes, you can check it in the monitor as mentioned by MD.

To store, you can do that programmatically

    int UID = android.os.Process.myUid();
    rxBytes += getUidRxBytes(UID);
    txBytes += getUidTxBytes(UID);
    /**
     * Read UID Rx Bytes
     *
     * @param uid
     * @return rxBytes
     */
    public Long getUidRxBytes(int uid) {
        BufferedReader reader;
        Long rxBytes = 0L;
        try {
            reader = new BufferedReader(new FileReader("/proc/uid_stat/" + uid
                    + "/tcp_rcv"));
            rxBytes = Long.parseLong(reader.readLine());
            reader.close();
        }
        catch (FileNotFoundException e) {
            rxBytes = TrafficStats.getUidRxBytes(uid);
            //e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return rxBytes;
    }

    /**
     * Read UID Tx Bytes
     *
     * @param uid
     * @return txBytes
     */
    public Long getUidTxBytes(int uid) {
        BufferedReader reader;
        Long txBytes = 0L;
        try {
            reader = new BufferedReader(new FileReader("/proc/uid_stat/" + uid
                    + "/tcp_snd"));
            txBytes = Long.parseLong(reader.readLine());
            reader.close();
        }
        catch (FileNotFoundException e) {
            txBytes = TrafficStats.getUidTxBytes(uid);
            //e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return txBytes;
    }
}


回答4:

Have a look: Android Monitor.

In that there are many topics that you can monitor.

You will find Network Monitor.

Displaying a Running App in the Network Monitor:

Follow these steps:

  • Connect a hardware device.
  • Display Android Monitor.
  • Click the Network tab.
  • Open an app project and run it on the hardware device.
  • To start the Network Monitor, click Pause Pause icon to deselect it.

Any network traffic begins to appear in the Network Monitor:

The Network Monitor adds up the amount of time it takes for the device to transmit and receive kilobytes of data. The y-axis is in kilobytes per second. The x-axis starts with seconds, and then minutes and seconds, and so on.

  • To stop the Network Monitor, click Pause Pause icon again to select it.

Reference: Android Monitor