to top
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()
static long getMobileRxBytes()
Get the total number of bytes received through the mobile interface.
static long getMobileRxPackets()
Get the total number of packets received through the mobile interface.
static long getMobileTxBytes()
Get the total number of bytes transmitted through the mobile interface.
static long getMobileTxPackets()
Get the total number of packets transmitted through the mobile interface.
static int getThreadStatsTag()
Get the active tag used when accounting Socket traffic originating from the current thread.
static long getTotalRxBytes()
Get the total number of bytes received through all network interfaces.
static long getTotalRxPackets()
Get the total number of packets received through all network interfaces.
static long getTotalTxBytes()
Get the total number of bytes sent through all network interfaces.
static long getTotalTxPackets()
Get the total number of packets sent through all network interfaces.
static long getUidRxBytes(int uid)
Get the number of bytes received through the network for this UID.
static long getUidRxPackets(int uid)
Get the number of packets (TCP segments + UDP) received through the network for this UID.
static long getUidTcpRxBytes(int uid)
Get the number of TCP payload bytes received for this UID.
static long getUidTcpRxSegments(int uid)
Get the number of TCP segments received for this UID.
static long getUidTcpTxBytes(int uid)
Get the number of TCP payload bytes sent for this UID.
static long getUidTcpTxSegments(int uid)
Get the number of TCP segments sent for this UID.
static long getUidTxBytes(int uid)
Get the number of bytes sent through the network for this UID.
static long getUidTxPackets(int uid)
Get the number of packets (TCP segments + UDP) sent through the network for this UID.
static long getUidUdpRxBytes(int uid)
Get the number of UDP payload bytes received for this UID.
static long getUidUdpRxPackets(int uid)
Get the number of UDP packets received for this UID.
static long getUidUdpTxBytes(int uid)
Get the number of UDP payload bytes sent for this UID.
static long getUidUdpTxPackets(int uid)
Get the number of UDP packets sent for this UID.
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

public static long getMobileRxBytes ()

Added in API level 8

Get the total number of bytes received through the mobile interface.

Returns
  • number of bytes. If the statistics are not supported by this device, UNSUPPORTED will be returned.

public static long getMobileRxPackets ()

Added in API level 8

Get the total number of packets received through the mobile interface.

Returns
  • number of packets. If the statistics are not supported by this device, UNSUPPORTED will be returned.

public static long getMobileTxBytes ()

Added in API level 8

Get the total number of bytes transmitted through the mobile interface.

Returns
  • number of bytes. If the statistics are not supported by this device, UNSUPPORTED will be returned.

public static long getMobileTxPackets ()

Added in API level 8

Get the total number of packets transmitted through the mobile interface.

Returns
  • number of packets. If the statistics are not supported by this device, UNSUPPORTED will be returned.

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

Get the total number of bytes received through all network interfaces.

Returns
  • number of bytes. If the statistics are not supported by this device, UNSUPPORTED will be returned.

public static long getTotalRxPackets ()

Added in API level 8

Get the total number of packets received through all network interfaces.

Returns
  • number of packets. If the statistics are not supported by this device, UNSUPPORTED will be returned.

public static long getTotalTxBytes ()

Added in API level 8

Get the total number of bytes sent through all network interfaces.

Returns
  • number of bytes. If the statistics are not supported by this device, UNSUPPORTED will be returned.

public static long getTotalTxPackets ()

Added in API level 8

Get the total number of packets sent through all network interfaces.

Returns
  • the number of packets. If the statistics are not supported by this device, UNSUPPORTED will be returned.

public static long getUidRxBytes (int uid)

Added in API level 8

Get the number of bytes received through the network for this UID. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of bytes
See Also

public static long getUidRxPackets (int uid)

Added in API level 12

Get the number of packets (TCP segments + UDP) received through the network for this UID. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of packets
See Also

public static long getUidTcpRxBytes (int uid)

Added in API level 12

Get the number of TCP payload bytes received for this UID. This total does not include protocol and control overheads at the transport and the lower layers of the networking stack. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of bytes. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

public static long getUidTcpRxSegments (int uid)

Added in API level 12

Get the number of TCP segments received for this UID. Does not include TCP control packets (SYN/ACKs/FIN/..). The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of TCP segments. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

public static long getUidTcpTxBytes (int uid)

Added in API level 12

Get the number of TCP payload bytes sent for this UID. This total does not include protocol and control overheads at the transport and the lower layers of the networking stack. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of bytes. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

public static long getUidTcpTxSegments (int uid)

Added in API level 12

Get the number of TCP segments sent for this UID. Does not include TCP control packets (SYN/ACKs/FIN/..). The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of TCP segments. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

public static long getUidTxBytes (int uid)

Added in API level 8

Get the number of bytes sent through the network for this UID. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of bytes. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

public static long getUidTxPackets (int uid)

Added in API level 12

Get the number of packets (TCP segments + UDP) sent through the network for this UID. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of packets. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

public static long getUidUdpRxBytes (int uid)

Added in API level 12

Get the number of UDP payload bytes received for this UID. This total does not include protocol and control overheads at the transport and the lower layers of the networking stack. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of bytes. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

public static long getUidUdpRxPackets (int uid)

Added in API level 12

Get the number of UDP packets received for this UID. Includes DNS responses. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of packets. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

public static long getUidUdpTxBytes (int uid)

Added in API level 12

Get the number of UDP payload bytes sent for this UID. This total does not include protocol and control overheads at the transport and the lower layers of the networking stack. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of bytes. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

public static long getUidUdpTxPackets (int uid)

Added in API level 12

Get the number of UDP packets sent for this UID. Includes DNS requests. The statistics are across all interfaces. .

Parameters
uid The UID of the process to examine.
Returns
  • number of packets. If the statistics are not supported by this device, UNSUPPORTED will be returned.
See Also

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.