ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { // There is a network connection } else { // No network connectivity }
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiInfo != null && wifiInfo.isConnected()) { // The device is connected to a Wi-Fi network } else { // The device is not connected to a Wi-Fi network }
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mobileInfo != null && mobileInfo.isConnected()) { // The device is connected to a mobile data network } else { // The device is not connected to a mobile data network }These examples demonstrate how to use the `getNetworkInfo` method to retrieve information about the device's active network connection. This method returns a `NetworkInfo` object, which contains details about the network type and connectivity status. The `ConnectivityManager` class is part of the `android.net` package library.