0

is there a way to get details about disconnected network using android broadcast receiver or networkcallback ?

EXTRA_NETWORK_INFO, getNetworkInfo(NetworkType) are deprecated.

getAllNetworks() Returns an array of all Network currently tracked by the framework but not the disconnected network.

  • [Did this doc help?](https://developer.android.com/training/monitoring-device-state/connectivity-monitoring). – Taseer Sep 09 '19 at 12:56
  • no, most of the api's are deprecated, i found only way to use is NetworkCallback, onLost(Network network) can be used to detect the lost network. I thought getNetworkInfo(Network network) can be used to get the details about the lost network, but in my case it is returning null :( – Sathish Kumar Sep 09 '19 at 13:47
  • NetworkInfo is deprecated from API 29 – Sathish Kumar Sep 10 '19 at 05:25
  • is there any other way to fetch interface details such as interface name, interface state etc ? – Sathish Kumar Sep 10 '19 at 05:25

2 Answers2

0

yes, first register Broadcast receiver like this:

final IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(receiver, intentFilter);

when receiver is:

new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // this part is trigered when there is some network changes, do checks here
            }
        };

also don't forget to unregister it.

Antonis Radz
  • 3,036
  • 1
  • 16
  • 34
0

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));
}
Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65