Content and app developers continue to find new ways for users to consume and share content on their devices. This drives higher mobile data usage, which can sometimes cause unexpected costs for consumers. Well-made apps can use the built-in Android capabilities to know when mobile data is available and to help the users manage their data use.

Access network information with these permissions

In order to access information about available networks, an Android app must declare the ACCESS_NETWORK_STATE permission in the manifest. This isn’t typically a problem for Android apps that are monetized with ads; in order to retrieve the ads or any other network data, an app already needs to request the INTERNET permission.

At a minimum, apps that use the network and detect available networks will need to request the following permissions:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Detect any available network

Android devices can detect the available networks, such as Wi-Fi and mobile data, from the carrier. In some instances, you may want to check for any available network. I have one app that uses such a small amount of data that I’m not worried about the amount of data, but I am concerned about the effect on the user experience when the app attempts to access data and times out.

The following code uses the ConnectivityManager to check all networks, and returns true if any of them are available. If no network is available, your app may wish to delay or cancel a network operation.

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public static boolean isAnyNetworkAvailable(final Context context) {

    boolean status = false;
    final ConnectivityManager connManager =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if(null != connManager) {
NetworkInfo[] allNetworks = connManager.getAllNetworkInfo();
        if(null != allNetworks) {
            for(NetworkInfo info: allNetworks) {
                if ( info.getState() == NetworkInfo.State.CONNECTED ) {
                    status = true;
                    break;
                }
           }
      }
  }
  return status;
}

Detect when Wi-Fi is available

Despite the rapid increase in speed for mobile (carrier) networks over the last few years, many users are still charged for the amount of data they use. On the other hand, data use over Wi-Fi networks may be faster and have no such usage charges. For this reason some apps (such as the app for Google Play) can delay high-bandwidth operations like downloading apps until you are connected to a Wi-Fi network, which in many cases will not incur usage charges. You can employ this same technique in your apps.

With the above code example, there are a few ways you can get more information about the active network. To determine if the active data network is metered (which is a sign that users may be charged for data use), use ConnectivityManager.isActiveNetworkMetered(). Or, to explicitly determine if the active network is Wi-Fi, use NetworkInfo.getType() to check if the connected network is Wi-Fi; if it is, the enumerated type will be TYPE_WIFI.

Since each app may require a different strategy to appropriately query and make use of mobile data networks, dig into ConnectivityManager to see what features you need to use.

Be well behaved

Mobile devices are increasingly hosting “always-on” apps that send and receive data all the time without this being immediately obvious to users. Well-behaved apps should respect a user’s desire to limit or control data use over the mobile (carrier) network, and use knowledge of available networks to improve the user experience.