The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Measuring Bandwidth Consumption

The first step towards addressing bandwidth concerns is to get a better picture of how much bandwidth you are actually consuming, when, and under what conditions. Only then will you be able to determine where your efforts need to be applied and whether those efforts are actually giving you positive results. This chapter will examine a handful of ways you can determine how much bandwidth you are really using in your application.

Prerequisites

Understanding this chapter requires that you have read the core chapters and understand how Android apps are set up and operate.

On-Device Measurement

Many times, you are best served by measuring your bandwidth consumption right on the device itself:

  1. This is your only option for gathering bandwidth metrics from copies of your app in end users’ hands, unless they invite you to their home or office and have you sniff on their personal network, which seems unlikely
  2. This is your only option for gathering bandwidth metrics when you are using mobile data plans (e.g., 3G) instead of WiFi, since you probably do not control the wireless telecommunications infrastructure in your area
  3. This is your simplest option for tying bandwidth metrics to events within your app or occurring on the device
  4. This is your only option for using bandwidth metrics to adjust your application behavior in real time, in addition to using the metrics to learn how best to adjust your code in future updates to the app

Hence, in addition to perhaps other off-device techniques, you really should consider one of the on-device approaches outlined in the following sections.

Yourself, via TrafficStats

The preceding chapter outlined how to use the TrafficStats class to collect metrics on the bandwidth consumed by applications (including yours) and for the device as a whole. This gives you the most flexibility, because you can write your own code to collect whatever portion of this data you need. It can address all of the bullets shown above, for example.

It is not perfect, though:

  1. It requires you to write your own code, adding yet more work to your plate
  2. Per-UID traffic data may or may not be available, depending upon the device

Data Usage Screen in Settings

For more casual use, the Settings app in most Android devices offers a “Data Usage” screen that shows how much bandwidth has been consumed over a period of time:

Settings, App, Data Usage Screen, Data Usage Graph
Figure 1001: Settings, App, Data Usage Screen, Data Usage Graph

Scrolling further down will give you details of what apps were involved in that data usage:

Settings, App, Data Usage Screen, Data Usage Blame List
Figure 1002: Settings, App, Data Usage Screen, Data Usage “Blame List”

Tapping on any one of those list items will give you a bit more detail, specifically how much of that bandwidth was consumed while the app was in the foreground or the background:

Settings, App, Data Usage Screen, Data Usage App Details
Figure 1003: Settings, App, Data Usage Screen, Data Usage App Details

Off-Device Measurement

The preview of this section will not appear here for a while, due to a time machine mishap.

Android Studio Profiler

The preview of this section took that left turn at Albuquerque.