# Print output for @column tags ?>
  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.
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   | 
  
        
        
        static
        
        
        void
     | 
  
    
      
      clearThreadStatsUid()
      
      
        Clear any active UID set to account   | 
  
        
        
        static
        
        
        int
     | 
  
    
      
      getAndSetThreadStatsTag(int tag)
      
      
        Set active tag to use when accounting   | 
  
        
        
        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
        
        
        long
     | 
  
    
      
      getRxPackets(String iface)
      
      
        Return the number of packets received on the specified interface since device boot.  | 
  
        
        
        static
        
        
        int
     | 
  
    
      
      getThreadStatsTag()
      
      
        Get the active tag used when accounting   | 
  
        
        
        static
        
        
        int
     | 
  
    
      
      getThreadStatsUid()
      
      
        Get the active UID used when accounting   | 
  
        
        
        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
     | 
  
    
      
      getTxPackets(String iface)
      
      
        Return the number of packets transmitted on the specified interface 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 is deprecated.
    Starting in   | 
  
        
        
        static
        
        
        long
     | 
  
    
      
      getUidTcpRxSegments(int uid)
      
      
        
      This method is deprecated.
    Starting in   | 
  
        
        
        static
        
        
        long
     | 
  
    
      
      getUidTcpTxBytes(int uid)
      
      
        
      This method is deprecated.
    Starting in   | 
  
        
        
        static
        
        
        long
     | 
  
    
      
      getUidTcpTxSegments(int uid)
      
      
        
      This method is deprecated.
    Starting in   | 
  
        
        
        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 is deprecated.
    Starting in   | 
  
        
        
        static
        
        
        long
     | 
  
    
      
      getUidUdpRxPackets(int uid)
      
      
        
      This method is deprecated.
    Starting in   | 
  
        
        
        static
        
        
        long
     | 
  
    
      
      getUidUdpTxBytes(int uid)
      
      
        
      This method is deprecated.
    Starting in   | 
  
        
        
        static
        
        
        long
     | 
  
    
      
      getUidUdpTxPackets(int uid)
      
      
        
      This method is deprecated.
    Starting in   | 
  
        
        
        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   | 
  
        
        
        static
        
        
        void
     | 
  
    
      
      setThreadStatsUid(int uid)
      
      
        Set specific UID to use when accounting   | 
  
        
        
        static
        
        
        void
     | 
  
    
      
      tagDatagramSocket(DatagramSocket socket)
      
      
        Tag the given   | 
  
        
        
        static
        
        
        void
     | 
  
    
      
      tagFileDescriptor(FileDescriptor fd)
      
      
        Tag the given   | 
  
        
        
        static
        
        
        void
     | 
  
    
      
      tagSocket(Socket socket)
      
      
        Tag the given   | 
  
        
        
        static
        
        
        void
     | 
  
    
      
      untagDatagramSocket(DatagramSocket socket)
      
      
        Remove any statistics parameters from the given   | 
  
        
        
        static
        
        
        void
     | 
  
    
      
      untagFileDescriptor(FileDescriptor fd)
      
      
        Remove any statistics parameters from the given   | 
  
        
        
        static
        
        
        void
     | 
  
    
      
      untagSocket(Socket socket)
      
      
        Remove any statistics parameters from the given   | 
  
Inherited methods | |
|---|---|
public static final int UNSUPPORTED
The return value to indicate that the device does not support the statistic.
Constant Value: -1 (0xffffffff)
public TrafficStats ()
public static void clearThreadStatsTag ()
Clear any active tag set to account Socket traffic originating
 from the current thread.
See also:
public static void clearThreadStatsUid ()
Clear any active UID set to account Socket traffic originating
 from the current thread.
See also:
public static int getAndSetThreadStatsTag (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(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.
| Parameters | |
|---|---|
tag | 
        
          int  | 
      
| Returns | |
|---|---|
int | 
        the current tag for the calling thread, which can be used to restore any existing values after a nested operation is finished | 
public static 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 Build.VERSION_CODES.JELLY_BEAN_MR2, this may
 return UNSUPPORTED on devices where statistics aren't available.
| Returns | |
|---|---|
long | 
        |
public static 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 Build.VERSION_CODES.JELLY_BEAN_MR2, this may
 return UNSUPPORTED on devices where statistics aren't available.
| Returns | |
|---|---|
long | 
        |
public static 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 Build.VERSION_CODES.JELLY_BEAN_MR2, this may
 return UNSUPPORTED on devices where statistics aren't available.
| Returns | |
|---|---|
long | 
        |
public static 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 Build.VERSION_CODES.JELLY_BEAN_MR2, this may
 return UNSUPPORTED on devices where statistics aren't available.
| Returns | |
|---|---|
long | 
        |
public static long getRxPackets (String iface)
Return the number of packets received on the specified interface since device boot. Statistics are measured at the network layer, so both TCP and UDP usage are included.
| Parameters | |
|---|---|
iface | 
        
          String: The name of the interface.
 This value cannot be null. | 
      
| Returns | |
|---|---|
long | 
        The number of received packets. | 
public static 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(java.net.Socket).
| Returns | |
|---|---|
int | 
        |
See also:
public static int getThreadStatsUid ()
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).
| Returns | |
|---|---|
int | 
        |
See also:
public static 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 Build.VERSION_CODES.JELLY_BEAN_MR2, this may
 return UNSUPPORTED on devices where statistics aren't available.
| Returns | |
|---|---|
long | 
        |
public static 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 Build.VERSION_CODES.JELLY_BEAN_MR2, this may
 return UNSUPPORTED on devices where statistics aren't available.
| Returns | |
|---|---|
long | 
        |
public static 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 Build.VERSION_CODES.JELLY_BEAN_MR2, this may
 return UNSUPPORTED on devices where statistics aren't available.
| Returns | |
|---|---|
long | 
        |
public static 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 Build.VERSION_CODES.JELLY_BEAN_MR2, this may
 return UNSUPPORTED on devices where statistics aren't available.
| Returns | |
|---|---|
long | 
        |
public static long getTxPackets (String iface)
Return the number of packets transmitted on the specified interface since device boot. Statistics are measured at the network layer, so both TCP and UDP usage are included.
| Parameters | |
|---|---|
iface | 
        
          String: The name of the interface.
 This value cannot be null. | 
      
| Returns | |
|---|---|
long | 
        The number of transmitted packets. | 
public static 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 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.
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static 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 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.
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static long getUidTcpRxBytes (int uid)
      This method is deprecated.
    Starting in Build.VERSION_CODES.JELLY_BEAN_MR2,
             transport layer statistics are no longer available, and will
             always return UNSUPPORTED.
  
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static long getUidTcpRxSegments (int uid)
      This method is deprecated.
    Starting in Build.VERSION_CODES.JELLY_BEAN_MR2,
             transport layer statistics are no longer available, and will
             always return UNSUPPORTED.
  
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static long getUidTcpTxBytes (int uid)
      This method is deprecated.
    Starting in Build.VERSION_CODES.JELLY_BEAN_MR2,
             transport layer statistics are no longer available, and will
             always return UNSUPPORTED.
  
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static long getUidTcpTxSegments (int uid)
      This method is deprecated.
    Starting in Build.VERSION_CODES.JELLY_BEAN_MR2,
             transport layer statistics are no longer available, and will
             always return UNSUPPORTED.
  
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static 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 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.
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static 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 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.
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static long getUidUdpRxBytes (int uid)
      This method is deprecated.
    Starting in Build.VERSION_CODES.JELLY_BEAN_MR2,
             transport layer statistics are no longer available, and will
             always return UNSUPPORTED.
  
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static long getUidUdpRxPackets (int uid)
      This method is deprecated.
    Starting in Build.VERSION_CODES.JELLY_BEAN_MR2,
             transport layer statistics are no longer available, and will
             always return UNSUPPORTED.
  
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static long getUidUdpTxBytes (int uid)
      This method is deprecated.
    Starting in Build.VERSION_CODES.JELLY_BEAN_MR2,
             transport layer statistics are no longer available, and will
             always return UNSUPPORTED.
  
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static long getUidUdpTxPackets (int uid)
      This method is deprecated.
    Starting in Build.VERSION_CODES.JELLY_BEAN_MR2,
             transport layer statistics are no longer available, and will
             always return UNSUPPORTED.
  
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
| Returns | |
|---|---|
long | 
        |
See also:
public static 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. | 
      
public static 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. | 
      
public static 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(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.
| Parameters | |
|---|---|
tag | 
        
          int  | 
      
See also:
public static void setThreadStatsUid (int uid)
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
 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).
| Parameters | |
|---|---|
uid | 
        
          int  | 
      
public static 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(java.net.DatagramSocket) to remove statistics
 parameters.
| Parameters | |
|---|---|
socket | 
        
          DatagramSocket  | 
      
| Throws | |
|---|---|
SocketException | 
          |
See also:
public static void tagFileDescriptor (FileDescriptor fd)
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  | 
      
| Throws | |
|---|---|
IOException | 
          |
See also:
public static 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(java.net.Socket) to remove
 statistics parameters.
| Parameters | |
|---|---|
socket | 
        
          Socket  | 
      
| Throws | |
|---|---|
SocketException | 
          |
See also:
public static void untagDatagramSocket (DatagramSocket socket)
Remove any statistics parameters from the given DatagramSocket.
| Parameters | |
|---|---|
socket | 
        
          DatagramSocket  | 
      
| Throws | |
|---|---|
SocketException | 
          |
public static void untagFileDescriptor (FileDescriptor fd)
Remove any statistics parameters from the given FileDescriptor
 socket.
| Parameters | |
|---|---|
fd | 
        
          FileDescriptor  | 
      
| Throws | |
|---|---|
IOException | 
          |
public static void untagSocket (Socket socket)
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  | 
      
| Throws | |
|---|---|
SocketException | 
          |