Designing Android apps to handle slow network speed

Developing your Android app to handle slow network speed is an important task to consider.

Why it might be important to you

There are many reasons why an Android developer should care about this

  1. Different networks around the world have different properties, we often forget that our devices are not connected to high speed internet 24/7, most especially if you are building an app that cuts across several countries and regions.
  2. App crashing when network drops in the middle of a remote call or perhaps if network changes while user is downloading something within the app.

In Android, there are ways to determine your connection speed

  1. Using NetworkInfo class, ConnectivityManager and TelephonyManager to determine your network type.
  2. Another test is to download some file from the internet & calculate how long it took vs number of bytes in the file.
  3. Use Facebook’s Network Connection Class library to figure out the quality of the current user’s internet connection on the fly.

Using NetworkInfo class, ConnectivityManager and TelephonyManager to determine your network type

Connection speed is usually slower on mobile data than on WiFi, but then that depends on your mobile data provider. Here, we check if connected to a WiFi network or mobile data, then further perform a check for which exact type of mobile data we are connected to.

Download some file from the internet & calculate how long it took vs number of bytes in the file

If you are connected on WiFi, you can assume that your connection would probably be fast, but this is not true in all cases, your WiFi network connection might go down temporarily or just reduce in speed. Network API will not indicate if your internet connection has dropped in the middle of your remote call.

  1. We subtract the end time from start time to get the actual download time in milliseconds, then divide by 1000 to get the seconds.
  2. We divide 1024 by time in seconds, to get the kilobyte per each sec download.
  3. You can determine if connection is poor, good or average by checking it against set of bandwidth range in KB per sec you have specified in the code.
  4. Also to get the download speed, we divide the file size by time taken to download.

A sample code below which shows each of those steps

Use Facebook’s Network Connection Class library

Facebook developed an open-sourced Network Connection Class library that lets apps figure quality of user’s connection on the fly.

POOR // Bandwidth under 150 kbps.
MODERATE // Bandwidth between 150 and 550 kbps.
GOOD // Bandwidth over 2000 kbps.
EXCELLENT // Bandwidth over 2000 kbps.
UNKNOWN // connection quality cannot be found.

Handling edge cases

Following the best practice can reduce issues you may face but there might still be problems relating to user going-in-and-out of networks. You should display a nice friendly message according to the current state of the network and turn off some features if possible.

If you liked this post, please do recommend it to other people by hitting the 💚 .

Mobile Developer, Native Android & iOS. I play around with RN, Electron.