TrafficStats
open class TrafficStats
kotlin.Any | |
↳ | 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 | |
---|---|
static Int |
The return value to indicate that the device does not support the statistic. |
Public constructors | |
---|---|
Public methods | |
---|---|
open static Unit |
Clear any active tag set to account |
open static Unit |
Clear any active UID set to account |
open static Int |
getAndSetThreadStatsTag(tag: Int) Set active tag to use when accounting |
open static Long |
Return number of bytes received across mobile networks since device boot. |
open static Long |
Return number of packets received across mobile networks since device boot. |
open static Long |
Return number of bytes transmitted across mobile networks since device boot. |
open static Long |
Return number of packets transmitted across mobile networks since device boot. |
open static Long |
getRxBytes(iface: String) Return the number of bytes received on the specified interface since the interface was created. |
open static Long |
getRxPackets(iface: String) Return the number of packets received on the specified interface since the interface was created. |
open static Int |
Get the active tag used when accounting |
open static Int |
Get the active UID used when accounting |
open static Long |
Return number of bytes received since device boot. |
open static Long |
Return number of packets received since device boot. |
open static Long |
Return number of bytes transmitted since device boot. |
open static Long |
Return number of packets transmitted since device boot. |
open static Long |
getTxBytes(iface: String) Return the number of bytes transmitted on the specified interface since the interface was created. |
open static Long |
getTxPackets(iface: String) Return the number of packets transmitted on the specified interface since the interface was created. |
open static Long |
getUidRxBytes(uid: Int) Return number of bytes received by the given UID since device boot. |
open static Long |
getUidRxPackets(uid: Int) Return number of packets received by the given UID since device boot. |
open static Long |
getUidTcpRxBytes(uid: Int) |
open static Long |
getUidTcpRxSegments(uid: Int) |
open static Long |
getUidTcpTxBytes(uid: Int) |
open static Long |
getUidTcpTxSegments(uid: Int) |
open static Long |
getUidTxBytes(uid: Int) Return number of bytes transmitted by the given UID since device boot. |
open static Long |
getUidTxPackets(uid: Int) Return number of packets transmitted by the given UID since device boot. |
open static Long |
getUidUdpRxBytes(uid: Int) |
open static Long |
getUidUdpRxPackets(uid: Int) |
open static Long |
getUidUdpTxBytes(uid: Int) |
open static Long |
getUidUdpTxPackets(uid: Int) |
open static Unit |
incrementOperationCount(operationCount: Int) Increment count of network operations performed under the accounting tag currently active on the calling thread. |
open static Unit |
incrementOperationCount(tag: Int, operationCount: Int) Increment count of network operations performed under the given accounting tag. |
open static Unit |
setThreadStatsTag(tag: Int) Set active tag to use when accounting |
open static Unit |
setThreadStatsUid(uid: Int) Set specific UID to use when accounting |
open static Unit |
tagDatagramSocket(socket: DatagramSocket) Tag the given |
open static Unit |
Tag the given |
open static Unit |
Tag the given |
open static Unit |
untagDatagramSocket(socket: DatagramSocket) Remove any statistics parameters from the given |
open static Unit |
Remove any statistics parameters from the given |
open static Unit |
untagSocket(socket: Socket) Remove any statistics parameters from the given |
Constants
UNSUPPORTED
static val UNSUPPORTED: Int
The return value to indicate that the device does not support the statistic.
Value: -1
Public constructors
Public methods
clearThreadStatsTag
open static fun clearThreadStatsTag(): Unit
Clear any active tag set to account Socket
traffic originating from the current thread.
See Also
clearThreadStatsUid
open static fun clearThreadStatsUid(): Unit
Clear any active UID set to account Socket
traffic originating from the current thread.
See Also
getAndSetThreadStatsTag
open static fun getAndSetThreadStatsTag(tag: Int): Int
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(java.net.Socket)
.
Tags between 0xFFFFFF00
and 0xFFFFFFFF
are reserved and used internally by system services like DownloadManager
when performing traffic on behalf of an application.
Return | |
---|---|
Int |
the current tag for the calling thread, which can be used to restore any existing values after a nested operation is finished |
getMobileRxBytes
open static fun getMobileRxBytes(): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
getMobileRxPackets
open static fun getMobileRxPackets(): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
getMobileTxBytes
open static fun getMobileTxBytes(): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
getMobileTxPackets
open static fun getMobileTxPackets(): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
getRxBytes
open static fun getRxBytes(iface: String): Long
Return the number of bytes received on the specified interface since the interface was created. Statistics are measured at the network layer, so both TCP and UDP usage are included. Note that the returned values are partial statistics that do not count data from several sources and do not apply several adjustments that are necessary for correctness, such as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to determine whether traffic is being transferred on the specific interface but are not a substitute for the more accurate statistics provided by the NetworkStatsManager
APIs.
Parameters | |
---|---|
iface |
String: The name of the interface. This value cannot be null . |
Return | |
---|---|
Long |
The number of received bytes. |
getRxPackets
open static fun getRxPackets(iface: String): Long
Return the number of packets received on the specified interface since the interface was created. Statistics are measured at the network layer, so both TCP and UDP usage are included. Note that the returned values are partial statistics that do not count data from several sources and do not apply several adjustments that are necessary for correctness, such as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to determine whether traffic is being transferred on the specific interface but are not a substitute for the more accurate statistics provided by the NetworkStatsManager
APIs.
Parameters | |
---|---|
iface |
String: The name of the interface. This value cannot be null . |
Return | |
---|---|
Long |
The number of received packets. |
getThreadStatsTag
open static fun getThreadStatsTag(): Int
Get the active tag used when accounting Socket
traffic originating from the current thread. Only one active tag per thread is supported. tagSocket(java.net.Socket)
.
See Also
getThreadStatsUid
open static fun getThreadStatsUid(): Int
Get the active UID used when accounting Socket
traffic originating from the current thread. Only one active tag per thread is supported. tagSocket(java.net.Socket)
.
See Also
getTotalRxBytes
open static fun getTotalRxBytes(): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
getTotalRxPackets
open static fun getTotalRxPackets(): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
getTotalTxBytes
open static fun getTotalTxBytes(): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
getTotalTxPackets
open static fun getTotalTxPackets(): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
getTxBytes
open static fun getTxBytes(iface: String): Long
Return the number of bytes transmitted on the specified interface since the interface was created. Statistics are measured at the network layer, so both TCP and UDP usage are included. Note that the returned values are partial statistics that do not count data from several sources and do not apply several adjustments that are necessary for correctness, such as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to determine whether traffic is being transferred on the specific interface but are not a substitute for the more accurate statistics provided by the NetworkStatsManager
APIs.
Parameters | |
---|---|
iface |
String: The name of the interface. This value cannot be null . |
Return | |
---|---|
Long |
The number of transmitted bytes. |
getTxPackets
open static fun getTxPackets(iface: String): Long
Return the number of packets transmitted on the specified interface since the interface was created. Statistics are measured at the network layer, so both TCP and UDP usage are included. Note that the returned values are partial statistics that do not count data from several sources and do not apply several adjustments that are necessary for correctness, such as adjusting for VPN apps, IPv6-in-IPv4 translation, etc. These values can be used to determine whether traffic is being transferred on the specific interface but are not a substitute for the more accurate statistics provided by the NetworkStatsManager
APIs.
Parameters | |
---|---|
iface |
String: The name of the interface. This value cannot be null . |
Return | |
---|---|
Long |
The number of transmitted packets. |
getUidRxBytes
open static fun getUidRxBytes(uid: Int): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
Starting in android.os.Build.VERSION_CODES#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
.
getUidRxPackets
open static fun getUidRxPackets(uid: Int): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
Starting in android.os.Build.VERSION_CODES#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
.
getUidTcpRxBytes
open static fungetUidTcpRxBytes(uid: Int): Long
Deprecated: Starting in android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always return UNSUPPORTED
.
See Also
getUidTcpRxSegments
open static fungetUidTcpRxSegments(uid: Int): Long
Deprecated: Starting in android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always return UNSUPPORTED
.
See Also
getUidTcpTxBytes
open static fungetUidTcpTxBytes(uid: Int): Long
Deprecated: Starting in android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always return UNSUPPORTED
.
See Also
getUidTcpTxSegments
open static fungetUidTcpTxSegments(uid: Int): Long
Deprecated: Starting in android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always return UNSUPPORTED
.
See Also
getUidTxBytes
open static fun getUidTxBytes(uid: Int): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
Starting in android.os.Build.VERSION_CODES#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
.
getUidTxPackets
open static fun getUidTxPackets(uid: Int): Long
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 android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, this may return UNSUPPORTED
on devices where statistics aren't available.
Starting in android.os.Build.VERSION_CODES#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
.
getUidUdpRxBytes
open static fungetUidUdpRxBytes(uid: Int): Long
Deprecated: Starting in android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always return UNSUPPORTED
.
See Also
getUidUdpRxPackets
open static fungetUidUdpRxPackets(uid: Int): Long
Deprecated: Starting in android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always return UNSUPPORTED
.
See Also
getUidUdpTxBytes
open static fungetUidUdpTxBytes(uid: Int): Long
Deprecated: Starting in android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always return UNSUPPORTED
.
See Also
getUidUdpTxPackets
open static fungetUidUdpTxPackets(uid: Int): Long
Deprecated: Starting in android.os.Build.VERSION_CODES#JELLY_BEAN_MR2
, transport layer statistics are no longer available, and will always return UNSUPPORTED
.
See Also
incrementOperationCount
open static fun incrementOperationCount(operationCount: Int): Unit
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
open static fun incrementOperationCount(
tag: Int,
operationCount: Int
): Unit
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
open static fun setThreadStatsTag(tag: Int): Unit
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(java.net.Socket)
.
Tags between 0xFFFFFF00
and 0xFFFFFFFF
are reserved and used internally by system services like DownloadManager
when performing traffic on behalf of an application.
See Also
setThreadStatsUid
open static fun setThreadStatsUid(uid: Int): Unit
Set specific UID to use when accounting Socket
traffic originating from the current thread. Designed for use when performing an operation on behalf of another application, or when another application is performing operations on your behalf.
Any app can accept blame for traffic performed on a socket originally created by another app by calling this method with the android.system.Os#getuid()
value. However, only apps holding the android.Manifest.permission#UPDATE_DEVICE_STATS
permission may assign blame to another UIDs.
Changes only take effect during subsequent calls to tagSocket(java.net.Socket)
.
tagDatagramSocket
open static fun tagDatagramSocket(socket: DatagramSocket): Unit
Tag the given DatagramSocket
with any statistics parameters active for the current thread. Subsequent calls always replace any existing parameters. When finished, call untagDatagramSocket(java.net.DatagramSocket)
to remove statistics parameters.
Parameters | |
---|---|
socket |
DatagramSocket: This value cannot be null . |
See Also
tagFileDescriptor
open static fun tagFileDescriptor(fd: FileDescriptor): Unit
Tag the given FileDescriptor
socket with any statistics parameters active for the current thread. Subsequent calls always replace any existing parameters. When finished, call untagFileDescriptor(java.io.FileDescriptor)
to remove statistics parameters.
Parameters | |
---|---|
fd |
FileDescriptor: This value cannot be null . |
See Also
tagSocket
open static fun tagSocket(socket: Socket): Unit
Tag the given Socket
with any statistics parameters active for the current thread. Subsequent calls always replace any existing parameters. When finished, call untagSocket(java.net.Socket)
to remove statistics parameters.
Parameters | |
---|---|
socket |
Socket: This value cannot be null . |
See Also
untagDatagramSocket
open static fun untagDatagramSocket(socket: DatagramSocket): Unit
Remove any statistics parameters from the given DatagramSocket
.
Parameters | |
---|---|
socket |
DatagramSocket: This value cannot be null . |
untagFileDescriptor
open static fun untagFileDescriptor(fd: FileDescriptor): Unit
Remove any statistics parameters from the given FileDescriptor
socket.
Parameters | |
---|---|
fd |
FileDescriptor: This value cannot be null . |
untagSocket
open static fun untagSocket(socket: Socket): Unit
Remove any statistics parameters from the given Socket
.
In Android 8.1 (API level 27) and lower, a socket is automatically untagged when it's sent to another process using binder IPC with a ParcelFileDescriptor
container. In Android 9.0 (API level 28) and higher, the socket tag is kept when the socket is sent to another process using binder IPC. You can mimic the previous behavior by calling untagSocket()
before sending the socket to another process.
Parameters | |
---|---|
socket |
Socket: This value cannot be null . |