如何查看我的Android应用程序的网络消耗?

时间:2022-08-21 07:11:32

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

我需要查看我的Android应用程序的互联网消费情况。在我的应用程序中,我调用了大量的Web服务API。

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

我想知道我的应用程序在完整的时间内以KB / MB的速度消耗互联网的​​程度。

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

我怎么检查?有没有工具来检查?

5 个解决方案

#1


25  

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

Android Studio 2.0在Android Monitor中引入新的网络部分,可以帮助您解决问题。

如何查看我的Android应用程序的网络消耗?

Tx == Transmit Bytes Rx == Receive Bytes

#2


9  

There are three ways...

有三种方式......

  1. You can view in Device/Emulator. Go to Setting -> Data usage, and find your application in the list 如何查看我的Android应用程序的网络消耗?

    您可以在设备/模拟器中查看。转到设置 - >数据使用情况,然后在列表中找到您的应用程序

  2. In Eclipse, select DDMS (perspective) -> Select your package from Devices (left side) -> Click on Network Statistics tab -> Click Start 如何查看我的Android应用程序的网络消耗?

    在Eclipse中,选择DDMS(透视图) - >从Devices(左侧)选择包 - >单击Network Statistics选项卡 - >单击Start

  3. As already answered, in Android Studio, go to Android Monitor (bottom tab) -> Network (tab) -> look for Tx (Transmit Data) / Rx (Receive Data) 如何查看我的Android应用程序的网络消耗?

    如前所述,在Android Studio中,转到Android监视器(底部选项卡) - >网络(选项卡) - >查找Tx(传输数据)/ Rx(接收数据)

#3


6  

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

出于查看目的,您可以在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


6  

Have a look: Android Monitor.

看看: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.
  • 显示Android监视器。
  • 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:

任何网络流量都开始出现在网络监视器中:

如何查看我的Android应用程序的网络消耗?

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.

网络监视器会增加设备传输和接收千字节数据所需的时间。 y轴以千字节/秒为单位。 x轴以秒开始,然后是分钟和秒,依此类推。

  • To stop the Network Monitor, click Pause Pause icon again to select it.
  • 要停止网络监视器,请再次单击“暂停暂停”图标以将其选中。

Reference: Android Monitor

参考:Android监视器

#5


6  

If you want to check your app network consumption with any tools then use to Android Studio 2.0 that in you can see. See below how to use it

如果您想使用任何工具检查您的应用网络消费,请使用您可以看到的Android Studio 2.0。请参阅下文如何使用它

Network Monitor

网络监视器

The Network Monitor makes it possible to track when your application is making network requests. Using this tool, you can monitor how and when your app transfers data, and optimize the underlying code appropriately.

网络监视器可以跟踪应用程序何时发出网络请求。使用此工具,您可以监控应用程序传输数据的方式和时间,并相应地优化基础代码。

By monitoring the frequency of data transfers, and the amount of data transferred during each connection, you can identify areas of your app that can be made more efficient and use less battery power. Generally, you should look for short spikes that can be delayed, or that could cause a later transfer to be preempted.

通过监控数据传输的频率以及每次连接期间传输的数据量,您可以识别应用中可以提高效率并减少电池电量的区域。通常,您应该查找可能延迟的短尖峰,或者可能导致稍后的传输被抢占的短尖峰。

Displaying a Running App in the Network Monitor

在网络监视器中显示正在运行的应用程序

Follow these steps:

按着这些次序:

  1. Connect a hardware device.
  2. 连接硬件设备。
  3. Display Android Monitor.
  4. 显示Android监视器。
  5. Click the Network tab.
  6. 单击“网络”选项卡。
  7. Open an app project and run it on the hardware device.
  8. 打开应用程序项目并在硬件设备上运行它。
  9. To start the Network Monitor, click Pause Pause icon to deselect it.
  10. 要启动网络监视器,请单击“暂停暂停”图标以取消选择它。

如何查看我的Android应用程序的网络消耗?

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.

网络监视器会增加设备传输和接收千字节数据所需的时间。 y轴以千字节/秒为单位。 x轴以秒开始,然后是分钟和秒,依此类推。

  1. To stop the Network Monitor, click Pause Pause icon again to select it.
  2. 要停止网络监视器,请再次单击“暂停暂停”图标以将其选中。

OR

You want to display network consumption with own app then use below example

您想使用自己的应用程序显示网络消耗,然后使用下面的示例

main.xml

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:textSize="16sp"
         android:textStyle="bold"
         android:gravity="center"
         android:paddingBottom="20dip"
         android:text="Traffic Stats Demo" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#00ff00"
        android:gravity="center"
        android:text="Transmit Bytes" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:gravity="center"
        android:text="0"
        android:id="@+id/TX"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#ff0000"
        android:gravity="center"
        android:text="Receive Bytes" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:gravity="center"
        android:text="0"
        android:id="@+id/RX"/>
 </LinearLayout>

Main.java

Main.java

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.net.TrafficStats;
 import android.os.Bundle;
 import android.os.Handler;
 import android.widget.TextView;

 public class Main extends Activity {
     private Handler mHandler = new Handler();
     private long mStartRX = 0;
     private long mStartTX = 0;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         mStartRX = TrafficStats.getTotalRxBytes();
         mStartTX = TrafficStats.getTotalTxBytes();
         if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
              AlertDialog.Builder alert = new AlertDialog.Builder(this);
              alert.setTitle("Uh Oh!");
              alert.setMessage("Your device does not support traffic stat monitoring.");
              alert.show();
         }
         else {
             mHandler.postDelayed(mRunnable, 1000);
         }
     }

     private final Runnable mRunnable = new Runnable() {
         public void run() {
              TextView RX = (TextView)findViewById(R.id.RX);
              TextView TX = (TextView)findViewById(R.id.TX);
              long rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;
              RX.setText(Long.toString(rxBytes));
              long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
              TX.setText(Long.toString(txBytes));
              mHandler.postDelayed(mRunnable, 1000);
         }
     };
 }

#1


25  

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

Android Studio 2.0在Android Monitor中引入新的网络部分,可以帮助您解决问题。

如何查看我的Android应用程序的网络消耗?

Tx == Transmit Bytes Rx == Receive Bytes

#2


9  

There are three ways...

有三种方式......

  1. You can view in Device/Emulator. Go to Setting -> Data usage, and find your application in the list 如何查看我的Android应用程序的网络消耗?

    您可以在设备/模拟器中查看。转到设置 - >数据使用情况,然后在列表中找到您的应用程序

  2. In Eclipse, select DDMS (perspective) -> Select your package from Devices (left side) -> Click on Network Statistics tab -> Click Start 如何查看我的Android应用程序的网络消耗?

    在Eclipse中,选择DDMS(透视图) - >从Devices(左侧)选择包 - >单击Network Statistics选项卡 - >单击Start

  3. As already answered, in Android Studio, go to Android Monitor (bottom tab) -> Network (tab) -> look for Tx (Transmit Data) / Rx (Receive Data) 如何查看我的Android应用程序的网络消耗?

    如前所述,在Android Studio中,转到Android监视器(底部选项卡) - >网络(选项卡) - >查找Tx(传输数据)/ Rx(接收数据)

#3


6  

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

出于查看目的,您可以在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


6  

Have a look: Android Monitor.

看看: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.
  • 显示Android监视器。
  • 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:

任何网络流量都开始出现在网络监视器中:

如何查看我的Android应用程序的网络消耗?

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.

网络监视器会增加设备传输和接收千字节数据所需的时间。 y轴以千字节/秒为单位。 x轴以秒开始,然后是分钟和秒,依此类推。

  • To stop the Network Monitor, click Pause Pause icon again to select it.
  • 要停止网络监视器,请再次单击“暂停暂停”图标以将其选中。

Reference: Android Monitor

参考:Android监视器

#5


6  

If you want to check your app network consumption with any tools then use to Android Studio 2.0 that in you can see. See below how to use it

如果您想使用任何工具检查您的应用网络消费,请使用您可以看到的Android Studio 2.0。请参阅下文如何使用它

Network Monitor

网络监视器

The Network Monitor makes it possible to track when your application is making network requests. Using this tool, you can monitor how and when your app transfers data, and optimize the underlying code appropriately.

网络监视器可以跟踪应用程序何时发出网络请求。使用此工具,您可以监控应用程序传输数据的方式和时间,并相应地优化基础代码。

By monitoring the frequency of data transfers, and the amount of data transferred during each connection, you can identify areas of your app that can be made more efficient and use less battery power. Generally, you should look for short spikes that can be delayed, or that could cause a later transfer to be preempted.

通过监控数据传输的频率以及每次连接期间传输的数据量,您可以识别应用中可以提高效率并减少电池电量的区域。通常,您应该查找可能延迟的短尖峰,或者可能导致稍后的传输被抢占的短尖峰。

Displaying a Running App in the Network Monitor

在网络监视器中显示正在运行的应用程序

Follow these steps:

按着这些次序:

  1. Connect a hardware device.
  2. 连接硬件设备。
  3. Display Android Monitor.
  4. 显示Android监视器。
  5. Click the Network tab.
  6. 单击“网络”选项卡。
  7. Open an app project and run it on the hardware device.
  8. 打开应用程序项目并在硬件设备上运行它。
  9. To start the Network Monitor, click Pause Pause icon to deselect it.
  10. 要启动网络监视器,请单击“暂停暂停”图标以取消选择它。

如何查看我的Android应用程序的网络消耗?

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.

网络监视器会增加设备传输和接收千字节数据所需的时间。 y轴以千字节/秒为单位。 x轴以秒开始,然后是分钟和秒,依此类推。

  1. To stop the Network Monitor, click Pause Pause icon again to select it.
  2. 要停止网络监视器,请再次单击“暂停暂停”图标以将其选中。

OR

You want to display network consumption with own app then use below example

您想使用自己的应用程序显示网络消耗,然后使用下面的示例

main.xml

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:textSize="16sp"
         android:textStyle="bold"
         android:gravity="center"
         android:paddingBottom="20dip"
         android:text="Traffic Stats Demo" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#00ff00"
        android:gravity="center"
        android:text="Transmit Bytes" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:gravity="center"
        android:text="0"
        android:id="@+id/TX"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#ff0000"
        android:gravity="center"
        android:text="Receive Bytes" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:gravity="center"
        android:text="0"
        android:id="@+id/RX"/>
 </LinearLayout>

Main.java

Main.java

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.net.TrafficStats;
 import android.os.Bundle;
 import android.os.Handler;
 import android.widget.TextView;

 public class Main extends Activity {
     private Handler mHandler = new Handler();
     private long mStartRX = 0;
     private long mStartTX = 0;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         mStartRX = TrafficStats.getTotalRxBytes();
         mStartTX = TrafficStats.getTotalTxBytes();
         if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
              AlertDialog.Builder alert = new AlertDialog.Builder(this);
              alert.setTitle("Uh Oh!");
              alert.setMessage("Your device does not support traffic stat monitoring.");
              alert.show();
         }
         else {
             mHandler.postDelayed(mRunnable, 1000);
         }
     }

     private final Runnable mRunnable = new Runnable() {
         public void run() {
              TextView RX = (TextView)findViewById(R.id.RX);
              TextView TX = (TextView)findViewById(R.id.TX);
              long rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;
              RX.setText(Long.toString(rxBytes));
              long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
              TX.setText(Long.toString(txBytes));
              mHandler.postDelayed(mRunnable, 1000);
         }
     };
 }