Android APIs
public class

TrafficStats

extends Object
java.lang.Object
   ↳ android.net.TrafficStats

Class Overview

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.

These statistics may not be available on all platforms. If the statistics are not supported by this device, UNSUPPORTED will be returned.

Summary

Constants
int UNSUPPORTED The return value to indicate that the device does not support the statistic.
Public Constructors
TrafficStats()
Public Methods
static void clearThreadStatsTag()
Clear any active tag set to account Socket traffic originating from the current thread.
static long getMobileRxBytes()
Return number of bytes received across mobile networks since device boot.
static long getMobileRxPackets()
Return number of packets received across mobile networks since device boot.
static long getMobileTxBytes()
Return number of bytes transmitted across mobile networks since device boot.
static long getMobileTxPackets()
Return number of packets transmitted across mobile networks since device boot.
static int getThreadStatsTag()
Get the active tag used when accounting Socket traffic originating from the current thread.
static long getTotalRxBytes()
Return number of bytes received since device boot.
static long getTotalRxPackets()
Return number of packets received since device boot.
static long getTotalTxBytes()
Return number of bytes transmitted since device boot.
static long getTotalTxPackets()
Return number of packets transmitted since device boot.
static long getUidRxBytes(int uid)
Return number of bytes received by the given UID since device boot.
static long getUidRxPackets(int uid)
Return number of packets received by the given UID since device boot.
static long getUidTcpRxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.
static long getUidTcpRxSegments(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.
static long getUidTcpTxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.
static long getUidTcpTxSegments(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.
static long getUidTxBytes(int uid)
Return number of bytes transmitted by the given UID since device boot.
static long getUidTxPackets(int uid)
Return number of packets transmitted by the given UID since device boot.
static long getUidUdpRxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.
static long getUidUdpRxPackets(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.
static long getUidUdpTxBytes(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.
static long getUidUdpTxPackets(int uid)
This method was deprecated in API level 18. Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.
static void incrementOperationCount(int tag, int operationCount)
Increment count of network operations performed under the given accounting tag.
static void incrementOperationCount(int operationCount)
Increment count of network operations performed under the accounting tag currently active on the calling thread.
static void setThreadStatsTag(int tag)
Set active tag to use when accounting Socket traffic originating from the current thread.
static void tagSocket(Socket socket)
Tag the given Socket with any statistics parameters active for the current thread.
static void untagSocket(Socket socket)
Remove any statistics parameters from the given Socket.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int UNSUPPORTED

Added in API level 8

The return value to indicate that the device does not support the statistic.

Constant Value: -1 (0xffffffff)

Public Constructors

public TrafficStats ()

Added in API level 8

Public Methods

public static void clearThreadStatsTag ()

Added in API level 14

Clear any active tag set to account Socket traffic originating from the current thread.

public static long getMobileRxBytes ()

Added in API level 8

Return number of bytes received across mobile networks since device boot. Counts packets across all mobile network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

public static long getMobileRxPackets ()

Added in API level 8

Return number of packets received across mobile networks since device boot. Counts packets across all mobile network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

public static long getMobileTxBytes ()

Added in API level 8

Return number of bytes transmitted across mobile networks since device boot. Counts packets across all mobile network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

public static long getMobileTxPackets ()

Added in API level 8

Return number of packets transmitted across mobile networks since device boot. Counts packets across all mobile network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

public static int getThreadStatsTag ()

Added in API level 14

Get the active tag used when accounting Socket traffic originating from the current thread. Only one active tag per thread is supported. tagSocket(Socket).

public static long getTotalRxBytes ()

Added in API level 8

Return number of bytes received since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

public static long getTotalRxPackets ()

Added in API level 8

Return number of packets received since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

public static long getTotalTxBytes ()

Added in API level 8

Return number of bytes transmitted since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

public static long getTotalTxPackets ()

Added in API level 8

Return number of packets transmitted since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

public static long getUidRxBytes (int uid)

Added in API level 8

Return number of bytes received by the given UID since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

See Also

public static long getUidRxPackets (int uid)

Added in API level 12

Return number of packets received by the given UID since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

See Also

public static long getUidTcpRxBytes (int uid)

Added in API level 12

This method was deprecated in API level 18.
Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.

public static long getUidTcpRxSegments (int uid)

Added in API level 12

This method was deprecated in API level 18.
Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.

public static long getUidTcpTxBytes (int uid)

Added in API level 12

This method was deprecated in API level 18.
Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.

public static long getUidTcpTxSegments (int uid)

Added in API level 12

This method was deprecated in API level 18.
Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.

public static long getUidTxBytes (int uid)

Added in API level 8

Return number of bytes transmitted by the given UID since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

See Also

public static long getUidTxPackets (int uid)

Added in API level 12

Return number of packets transmitted by the given UID since device boot. Counts packets across all network interfaces, and always increases monotonically since device boot. Statistics are measured at the network layer, so they include both TCP and UDP usage.

Before JELLY_BEAN_MR2, this may return UNSUPPORTED on devices where statistics aren't available.

See Also

public static long getUidUdpRxBytes (int uid)

Added in API level 12

This method was deprecated in API level 18.
Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.

public static long getUidUdpRxPackets (int uid)

Added in API level 12

This method was deprecated in API level 18.
Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.

public static long getUidUdpTxBytes (int uid)

Added in API level 12

This method was deprecated in API level 18.
Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.

public static long getUidUdpTxPackets (int uid)

Added in API level 12

This method was deprecated in API level 18.
Starting in JELLY_BEAN_MR2, transport layer statistics are no longer available, and will always return UNSUPPORTED.

public static void incrementOperationCount (int tag, int operationCount)

Added in API level 14

Increment count of network operations performed under the given accounting tag. This can be used to derive bytes-per-operation.

Parameters
tag Accounting tag used in setThreadStatsTag(int).
operationCount Number of operations to increment count by.

public static void incrementOperationCount (int operationCount)

Added in API level 14

Increment count of network operations performed under the accounting tag currently active on the calling thread. This can be used to derive bytes-per-operation.

Parameters
operationCount Number of operations to increment count by.

public static void setThreadStatsTag (int tag)

Added in API level 14

Set active tag to use when accounting Socket traffic originating from the current thread. Only one active tag per thread is supported.

Changes only take effect during subsequent calls to tagSocket(Socket).

Tags between 0xFFFFFF00 and 0xFFFFFFFF are reserved and used internally by system services like DownloadManager when performing traffic on behalf of an application.

public static void tagSocket (Socket socket)

Added in API level 14

Tag the given Socket with any statistics parameters active for the current thread. Subsequent calls always replace any existing parameters. When finished, call untagSocket(Socket) to remove statistics parameters.

public static void untagSocket (Socket socket)

Added in API level 14

Remove any statistics parameters from the given Socket.