Download source code of this article

Android API version 8 (Version 2.2) includes a convenient class, TrafficStats, to retrieve the traffic statistics of the device. This class provides traffic statistics of both Mobile Interface and All Network Interface. This class has following methods:

static long getMobileRxBytes()
static long getMobileRxPackets()
static long getMobileTxBytes()
static long getMobileTxPackets()
static long getTotalRxBytes()
static long getTotalRxPackets()
static long getTotalTxBytes()
static long getTotalTxPackets()
static long getUidRxBytes(int uid)
static long getUidTxBytes(int uid)

The function names explains its purpose. For more detailed documentation visit this link. Note that this class is only available from API version 8 onwards.