public class TrafficStats extends Object
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.
Modifier and Type | Field and Description |
---|---|
static long |
GB_IN_BYTES |
static long |
KB_IN_BYTES |
static long |
MB_IN_BYTES |
static long |
PB_IN_BYTES |
static int |
TAG_SYSTEM_BACKUP
Default tag value for
BackupManager backup traffic; that is,
traffic from the device to the storage backend. |
static int |
TAG_SYSTEM_DOWNLOAD
Default tag value for
DownloadManager traffic. |
static int |
TAG_SYSTEM_MEDIA
Default tag value for
MediaPlayer traffic. |
static int |
TAG_SYSTEM_RESTORE
Default tag value for
BackupManager restore traffic; that is,
app data retrieved from the storage backend at install time. |
static long |
TB_IN_BYTES |
static int |
UID_REMOVED
Special UID value used when collecting
NetworkStatsHistory for
removed applications. |
static int |
UID_TETHERING
Special UID value used when collecting
NetworkStatsHistory for
tethering traffic. |
static int |
UNSUPPORTED
The return value to indicate that the device does not support the statistic.
|
Constructor and Description |
---|
TrafficStats() |
Modifier and Type | Method and Description |
---|---|
static void |
clearThreadStatsTag()
Clear any active tag set to account
Socket traffic originating
from the current thread. |
static void |
clearThreadStatsUid()
Clear any active UID set to account
Socket traffic originating
from the current thread. |
static void |
closeQuietly(INetworkStatsSession session) |
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 |
getMobileTcpRxPackets() |
static long |
getMobileTcpTxPackets() |
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 long |
getRxBytes(String iface) |
static long |
getRxPackets(String iface) |
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 |
getTxBytes(String iface) |
static long |
getTxPackets(String iface) |
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)
Deprecated.
Starting in
Build.VERSION_CODES.JELLY_BEAN_MR2 ,
transport layer statistics are no longer available, and will
always return UNSUPPORTED . |
static long |
getUidTcpRxSegments(int uid)
Deprecated.
Starting in
Build.VERSION_CODES.JELLY_BEAN_MR2 ,
transport layer statistics are no longer available, and will
always return UNSUPPORTED . |
static long |
getUidTcpTxBytes(int uid)
Deprecated.
Starting in
Build.VERSION_CODES.JELLY_BEAN_MR2 ,
transport layer statistics are no longer available, and will
always return UNSUPPORTED . |
static long |
getUidTcpTxSegments(int uid)
Deprecated.
Starting in
Build.VERSION_CODES.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)
Deprecated.
Starting in
Build.VERSION_CODES.JELLY_BEAN_MR2 ,
transport layer statistics are no longer available, and will
always return UNSUPPORTED . |
static long |
getUidUdpRxPackets(int uid)
Deprecated.
Starting in
Build.VERSION_CODES.JELLY_BEAN_MR2 ,
transport layer statistics are no longer available, and will
always return UNSUPPORTED . |
static long |
getUidUdpTxBytes(int uid)
Deprecated.
Starting in
Build.VERSION_CODES.JELLY_BEAN_MR2 ,
transport layer statistics are no longer available, and will
always return UNSUPPORTED . |
static long |
getUidUdpTxPackets(int uid)
Deprecated.
Starting in
Build.VERSION_CODES.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 |
setThreadStatsTagBackup()
Set active tag to use when accounting
Socket traffic originating
from the current thread. |
static void |
setThreadStatsTagRestore()
Set active tag to use when accounting
Socket traffic originating
from the current thread. |
static void |
setThreadStatsUid(int uid)
Set specific UID to use when accounting
Socket traffic
originating from the current thread. |
static void |
startDataProfiling(Context context)
Start profiling data usage for current UID.
|
static NetworkStats |
stopDataProfiling(Context context)
Stop profiling data usage for current UID.
|
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 . |
public static final int UNSUPPORTED
public static final long KB_IN_BYTES
public static final long MB_IN_BYTES
public static final long GB_IN_BYTES
public static final long TB_IN_BYTES
public static final long PB_IN_BYTES
public static final int UID_REMOVED
NetworkStatsHistory
for
removed applications.public static final int UID_TETHERING
NetworkStatsHistory
for
tethering traffic.public static final int TAG_SYSTEM_DOWNLOAD
DownloadManager
traffic.public static final int TAG_SYSTEM_MEDIA
MediaPlayer
traffic.public static final int TAG_SYSTEM_BACKUP
BackupManager
backup traffic; that is,
traffic from the device to the storage backend.public static final int TAG_SYSTEM_RESTORE
BackupManager
restore traffic; that is,
app data retrieved from the storage backend at install time.public static void setThreadStatsTag(int tag)
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.
clearThreadStatsTag()
public static void setThreadStatsTagBackup()
Socket
traffic originating
from the current thread. The tag used internally is well-defined to
distinguish all backup-related traffic.public static void setThreadStatsTagRestore()
Socket
traffic originating
from the current thread. The tag used internally is well-defined to
distinguish all restore-related traffic.public static int getThreadStatsTag()
Socket
traffic originating
from the current thread. Only one active tag per thread is supported.
tagSocket(Socket)
.setThreadStatsTag(int)
public static void clearThreadStatsTag()
Socket
traffic originating
from the current thread.setThreadStatsTag(int)
public static void setThreadStatsUid(int uid)
Socket
traffic
originating from the current thread. Designed for use when performing an
operation on behalf of another application.
Changes only take effect during subsequent calls to
tagSocket(Socket)
.
To take effect, caller must hold
android.Manifest.permission#UPDATE_DEVICE_STATS
permission.
public static void clearThreadStatsUid()
Socket
traffic originating
from the current thread.setThreadStatsUid(int)
public static void tagSocket(Socket socket) throws SocketException
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.SocketException
setThreadStatsTag(int)
public static void untagSocket(Socket socket) throws SocketException
Socket
.SocketException
public static void tagDatagramSocket(DatagramSocket socket) throws SocketException
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.SocketException
setThreadStatsTag(int)
public static void untagDatagramSocket(DatagramSocket socket) throws SocketException
DatagramSocket
.SocketException
public static void startDataProfiling(Context context)
public static NetworkStats stopDataProfiling(Context context)
NetworkStats
of data that occurred since last
startDataProfiling(Context)
call.public static void incrementOperationCount(int operationCount)
operationCount
- Number of operations to increment count by.public static void incrementOperationCount(int tag, int operationCount)
tag
- Accounting tag used in setThreadStatsTag(int)
.operationCount
- Number of operations to increment count by.public static void closeQuietly(INetworkStatsSession session)
public static long getMobileTxPackets()
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may
return UNSUPPORTED
on devices where statistics aren't available.
public static long getMobileRxPackets()
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may
return UNSUPPORTED
on devices where statistics aren't available.
public static long getMobileTxBytes()
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may
return UNSUPPORTED
on devices where statistics aren't available.
public static long getMobileRxBytes()
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may
return UNSUPPORTED
on devices where statistics aren't available.
public static long getMobileTcpRxPackets()
public static long getMobileTcpTxPackets()
public static long getTxPackets(String iface)
public static long getRxPackets(String iface)
public static long getTxBytes(String iface)
public static long getRxBytes(String iface)
public static long getTotalTxPackets()
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may
return UNSUPPORTED
on devices where statistics aren't available.
public static long getTotalRxPackets()
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may
return UNSUPPORTED
on devices where statistics aren't available.
public static long getTotalTxBytes()
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may
return UNSUPPORTED
on devices where statistics aren't available.
public static long getTotalRxBytes()
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may
return UNSUPPORTED
on devices where statistics aren't available.
public static long getUidTxBytes(int uid)
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may
return UNSUPPORTED
on devices where statistics aren't available.
Starting in 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
.
Process.myUid()
,
ApplicationInfo.uid
public static long getUidRxBytes(int uid)
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may return
UNSUPPORTED
on devices where statistics aren't available.
Starting in 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
.
Process.myUid()
,
ApplicationInfo.uid
public static long getUidTxPackets(int uid)
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may return
UNSUPPORTED
on devices where statistics aren't available.
Starting in 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
.
Process.myUid()
,
ApplicationInfo.uid
public static long getUidRxPackets(int uid)
Before Build.VERSION_CODES.JELLY_BEAN_MR2
, this may return
UNSUPPORTED
on devices where statistics aren't available.
Starting in 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
.
Process.myUid()
,
ApplicationInfo.uid
@Deprecated public static long getUidTcpTxBytes(int uid)
Build.VERSION_CODES.JELLY_BEAN_MR2
,
transport layer statistics are no longer available, and will
always return UNSUPPORTED
.getUidTxBytes(int)
@Deprecated public static long getUidTcpRxBytes(int uid)
Build.VERSION_CODES.JELLY_BEAN_MR2
,
transport layer statistics are no longer available, and will
always return UNSUPPORTED
.getUidRxBytes(int)
@Deprecated public static long getUidUdpTxBytes(int uid)
Build.VERSION_CODES.JELLY_BEAN_MR2
,
transport layer statistics are no longer available, and will
always return UNSUPPORTED
.getUidTxBytes(int)
@Deprecated public static long getUidUdpRxBytes(int uid)
Build.VERSION_CODES.JELLY_BEAN_MR2
,
transport layer statistics are no longer available, and will
always return UNSUPPORTED
.getUidRxBytes(int)
@Deprecated public static long getUidTcpTxSegments(int uid)
Build.VERSION_CODES.JELLY_BEAN_MR2
,
transport layer statistics are no longer available, and will
always return UNSUPPORTED
.getUidTxPackets(int)
@Deprecated public static long getUidTcpRxSegments(int uid)
Build.VERSION_CODES.JELLY_BEAN_MR2
,
transport layer statistics are no longer available, and will
always return UNSUPPORTED
.getUidRxPackets(int)
@Deprecated public static long getUidUdpTxPackets(int uid)
Build.VERSION_CODES.JELLY_BEAN_MR2
,
transport layer statistics are no longer available, and will
always return UNSUPPORTED
.getUidTxPackets(int)
@Deprecated public static long getUidUdpRxPackets(int uid)
Build.VERSION_CODES.JELLY_BEAN_MR2
,
transport layer statistics are no longer available, and will
always return UNSUPPORTED
.getUidRxPackets(int)