Yes. NetworkInfo class is deprecated from API Level 29 . Instead, the ConnectivityManager class can be helpful here. It helps in monitoring the network connections, and notify the application when network connectivity changes or when connectivity to a network is lost. It also facilitates by allowing applications to know the state of the available networks and allows applications to request and select networks for their data traffic.
However, the below list of methods are deprecated in ConnectvityManager class:
getNetworkInfo returns connection status about a particular
network type.
getAllNetworkInfo returns connection status information of all network types supported by device.
getActiveNetworkInfo returns currently active default data network
network.
The ConnectivityManager.NetworkCallback is currently actively available to determine the network change status. This base class for NetworkRequest callbacks is used for notifications about network changes which can be extended by applications that are in need of network change notifications.
The below list of methods are supported :
getAllNetworks returns an array of all networks currently
tracked.
getActiveNetwork returns a network object
of currently active default data network.
The NetworkCapabilities class shall help in representation of the capabilities of an active network.
In general, whenever the system invokes onAvailable(Network), it shall pass the network that is available and whenever the system invokes onLost(Network), it shall pass the network that was lost which refers to the particular network that was lost (disconnected) and the argument tells you which network got lost (disconnected).
The onCapabilitiesChanged gets invoked immediately after onAvailable and this can help in determining capabilities of the available network like whether it is cellular network or a WiFi network by querying the NetworkCapabilities with hasTransport() and the appropriate transport constant like TRANSPORT_CELLULAR or TRANSPORT_WIFI (network of interest).
The below snippet takes into consideration the above information and helps in determining whether you have cellular or wifi network connectivity which in turn enables one to confirm whether the disconnected network(lost) is indeed not in use.
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Network network = connectivityManager.getActiveNetwork();
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI));
}