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 && activeNetwork.isConnectedOrConnecting();
Before performing any request, we should enable our application to check whether the Internet connection is active or not. This is not only a measure that contributes...