Determine whether you have an internet connection / Monitor Network Connectivity in Android

This post details about the code required to implement to check if you have an internet connection or whether you are connected to active network and what is the type of network such as WiFi or Ethernet so that you can perform network dependent tasks with assured success or show failure cases to the user if network is not connected or gets disconnected in runtime.

Note: getActiveNetworkInfo() was deprecated in Android 10. Use NetworkCallbacks instead for apps that target Android 10 (API level 29) and higher.

[bash] public class NetworkInfoStatus { NetworkInfoStatus() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); intentFilter.addAction("android.net.wifi.WIFI_AP_STATE_CHANGED"); registerReceiver(networkConnectivityReceiver, intentFilter); } BroadcastReceiver networkConnectivityReceiver = new BroadcastReceiver() { @Override public void onReceive(final Context context, final Intent intent) { Log.d(TAG,"received intent:" + intent.getAction()); if(intent.getExtras() != null) { ConnectivityManager cm = ( ConnectivityManager ) context.getSystemService ( Context.CONNECTIVITY_SERVICE ); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (activeNetwork != null) { if (activeNetwork.isConnectedOrConnecting()) { if (activeNetwork.isConnected()) { Log.d ( TAG ,"Network Type: " + activeNetwork.getTypeName()); } } } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) { Log.d ( TAG ," No Active Internet / Network Found"); } } } } } [/bash]

You can also use following code as reference to do additional check and identify type of network as well as IP Address of the device.

Check Network Type if its Stable network [ Ref. MediaResourceGetter.java from Samsung github ]

[bash] /** * @return true if the device is on an ethernet or wifi network. * If anything goes wrong (e.g., permission denied while trying to access * the network state), returns false. */ @VisibleForTesting boolean isNetworkReliable(Context context) { if (context.checkCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) { Log.w(TAG, "permission denied to access network state"); return false; } Integer networkType = getNetworkType(context); if (networkType == null) { return false; } switch (networkType.intValue()) { case ConnectivityManager.TYPE_ETHERNET: case ConnectivityManager.TYPE_WIFI: Log.d(TAG, "ethernet/wifi connection detected"); return true; case ConnectivityManager.TYPE_WIMAX: case ConnectivityManager.TYPE_MOBILE: default: Log.d(TAG, "no ethernet/wifi connection detected"); return false; } } [/bash]

Check Local Network IP Address [ Ref. oracle docs]

[bash] private static final int IPV6_LENGTH = 16; public static String getLocalNetworkIPAddress() { String ipAddress = ""; try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface netIfce = en.nextElement(); for (Enumeration<InetAddress> enumInetAddr = netIfce.getInetAddresses(); inetAddr.hasMoreElements(); ) { InetAddress inetAddress = enumInetAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().length() < IPV6_LENGTH) { ipAddress = inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { e.printStackTrace(); } return ipAddress; } [/bash]

Reference

Leave a Comment