Recent Changes‎ > ‎

Detailed Network Usage in DDMS

posted Feb 21, 2012, 2:40 PM by Xavier Ducrohet
In Android 4.0, the Data Usage feature in Settings enables long-term monitoring of how an application uses network resources. Starting with Android 4.0.3 and the upcoming DDMS r17, you can watch how an application uses network resources in real-time. You can also distinguish between different traffic types by applying a “tag” to network sockets before use. These tags are shown in a stack area chart in DDMS, as shown below:

In code, you can set tags on a per-thread basis with TrafficStats.setThreadStatsTag(), then manually tag sockets using TrafficStats.tagSocket() and untagSocket(). Alternatively, the Apache HttpClient and URLConnection libraries included in the platform will automatically tag sockets internally based on the current getThreadStatsTag() value. (These libraries correctly tag/untag sockets when recycled through keep-alive pools.) Here’s a typical example:

try {
// make network request using HttpClient.execute()
} finally {

Socket tagging is supported in Android 4.0, but displaying real-time stats is only available for devices running Android 4.0.3 or higher.