Avoiding useless network requests

Developers barely check the network status in the real world. Many of the alarms, broadcasts, and repetitive tasks we perform have to deal with an Internet connection. But if there is no active Internet connection, what is the purpose of performing all those operations? It would be more efficient to ignore all those operations until the Internet connection is back on track and working.

Determining the current Internet connection can be easily done with the following code snippet:

ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService (Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null ...

Get Android High Performance Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.