Most visited

Recently visited

Added in API level 8

TrafficStats

public class TrafficStats
extends Object

java.lang.Object
   ↳ android.net.TrafficStats


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.

Note that the statistics returned by this class reset and start from zero after every reboot. To access more robust historical network statistics data, use NetworkStatsManager instead.

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

Increment count of network operations performed under the accounting tag currently active on the calling thread.

static void incrementOperationCount(int tag, int operationCount)

Increment count of network operations performed under the given accounting tag.

static void setThreadStatsTag(int tag)

Set active tag to use when accounting Socket traffic originating from the current thread.

static void tagDatagramSocket(DatagramSocket socket)

Tag the given DatagramSocket with any statistics parameters active for the current thread.

static void tagSocket(Socket socket)

Tag the given Socket with any statistics parameters active for the current thread.

static void untagDatagramSocket(DatagramSocket socket)

Remove any statistics parameters from the given DatagramSocket.

static void untagSocket(Socket socket)

Remove any statistics parameters from the given Socket.

Inherited methods

From class java.lang.Object

Constants

UNSUPPORTED

Added in API level 8
int UNSUPPORTED

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

Constant Value: -1 (0xffffffff)

Public constructors

TrafficStats

Added in API level 8
TrafficStats ()

Public methods

clearThreadStatsTag

Added in API level 14
void clearThreadStatsTag ()

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

See also:

getMobileRxBytes

Added in API level 8
long getMobileRxBytes ()

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.

Returns
long

getMobileRxPackets

Added in API level 8
long getMobileRxPackets ()

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.

Returns
long

getMobileTxBytes

Added in API level 8
long getMobileTxBytes ()

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.

Returns
long

getMobileTxPackets

Added in API level 8
long getMobileTxPackets ()

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.

Returns
long

getThreadStatsTag

Added in API level 14
int getThreadStatsTag ()

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

Returns
int

See also:

getTotalRxBytes

Added in API level 8
long getTotalRxBytes ()

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.

Returns
long

getTotalRxPackets

Added in API level 8
long getTotalRxPackets ()

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.

Returns
long

getTotalTxBytes

Added in API level 8
long getTotalTxBytes ()

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.

Returns
long

getTotalTxPackets

Added in API level 8
long getTotalTxPackets ()

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.

Returns
long

getUidRxBytes

Added in API level 8
long getUidRxBytes (int uid)

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.

Starting in N this will only report traffic statistics for the calling UID. It will return UNSUPPORTED for all other UIDs for privacy reasons. To access historical network statistics belonging to other UIDs, use NetworkStatsManager.

Parameters
uid int
Returns
long

See also:

getUidRxPackets

Added in API level 12
long getUidRxPackets (int uid)

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.

Starting in N this will only report traffic statistics for the calling UID. It will return UNSUPPORTED for all other UIDs for privacy reasons. To access historical network statistics belonging to other UIDs, use NetworkStatsManager.

Parameters
uid int
Returns
long

See also:

getUidTcpRxBytes

Added in API level 12
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.

Parameters
uid int
Returns
long

See also:

getUidTcpRxSegments

Added in API level 12
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.

Parameters
uid int
Returns
long

See also:

getUidTcpTxBytes

Added in API level 12
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.

Parameters
uid int
Returns
long

See also:

getUidTcpTxSegments

Added in API level 12
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.

Parameters
uid int
Returns
long

See also:

getUidTxBytes

Added in API level 8
long getUidTxBytes (int uid)

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.

Starting in N this will only report traffic statistics for the calling UID. It will return UNSUPPORTED for all other UIDs for privacy reasons. To access historical network statistics belonging to other UIDs, use NetworkStatsManager.

Parameters
uid int
Returns
long

See also:

getUidTxPackets

Added in API level 12
long getUidTxPackets (int uid)

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.

Starting in N this will only report traffic statistics for the calling UID. It will return UNSUPPORTED for all other UIDs for privacy reasons. To access historical network statistics belonging to other UIDs, use NetworkStatsManager.

Parameters
uid int
Returns
long

See also:

getUidUdpRxBytes

Added in API level 12
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.

Parameters
uid int
Returns
long

See also:

getUidUdpRxPackets

Added in API level 12
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.

Parameters
uid int
Returns
long

See also:

getUidUdpTxBytes

Added in API level 12
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.

Parameters
uid int
Returns
long

See also:

getUidUdpTxPackets

Added in API level 12
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.

Parameters
uid int
Returns
long

See also:

incrementOperationCount

Added in API level 14
void incrementOperationCount (int operationCount)

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 int: Number of operations to increment count by.

incrementOperationCount

Added in API level 14
void incrementOperationCount (int tag, 
                int operationCount)

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

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

setThreadStatsTag

Added in API level 14
void setThreadStatsTag (int tag)

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.

Parameters
tag int

See also:

tagDatagramSocket

Added in API level 24
void tagDatagramSocket (DatagramSocket socket)

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

Parameters
socket DatagramSocket
Throws
SocketException

See also:

tagSocket

Added in API level 14
void tagSocket (Socket socket)

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.

Parameters
socket Socket
Throws
SocketException

See also:

untagDatagramSocket

Added in API level 24
void untagDatagramSocket (DatagramSocket socket)

Remove any statistics parameters from the given DatagramSocket.

Parameters
socket DatagramSocket
Throws
SocketException

untagSocket

Added in API level 14
void untagSocket (Socket socket)

Remove any statistics parameters from the given Socket.

Parameters
socket Socket
Throws
SocketException

Hooray!