7 Mar 2012

Check Internet Connection in Android

Hi All,

There are many features or snippet of code, which we are using in our daily development.
Here I am sharing some common basic function for make our life easy and fast in the development. Below is the code written in the Kotlin.

Permission: Add permission in to the Android manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

API level 29 or above use below function which is written in Kotlin.

fun isOnline(context: Context?): Boolean
    {
        var result = false
        val connectivityManager = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            connectivityManager?.run {
                connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)?.run {
                    result = when {
                        hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
                        hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
                        hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
                        else -> false
                    }
                }
            }
        } else {
            connectivityManager?.run {
                connectivityManager.activeNetworkInfo?.run {
                    if (type == ConnectivityManager.TYPE_WIFI) {
                        result = true
                    } else if (type == ConnectivityManager.TYPE_MOBILE) {
                        result = true
                    }
                }
            }
        }
        return result
    }

NOTE : activeNetworkInfo in now deprecated on API level 29. so below method is no loger use.

public boolean isOnline()
       {
              ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
              NetworkInfo netInfo = cm.getActiveNetworkInfo();

              if (netInfo != null && netInfo.isConnectedOrConnecting()&& cm.getActiveNetworkInfo().isAvailable()&& cm.getActiveNetworkInfo().isConnected())
              {
                     return true;
              }
              return false;
       } 


//There is another method for Internet connection

   public static boolean haveNetworkConnection(final Context context)
       {
              boolean haveConnectedWifi = false;
              boolean haveConnectedMobile = false;

              final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
              if (cm != null)
              {
                     final NetworkInfo[] netInfo = cm.getAllNetworkInfo();
                     for (final NetworkInfo netInfoCheck : netInfo)
                     {
                           if (netInfoCheck.getTypeName().equalsIgnoreCase("WIFI"))
                           {
                                  if (netInfoCheck.isConnected())
                                  {
                                         haveConnectedWifi = true;
                                  }
                           }
                           if (netInfoCheck.getTypeName().equalsIgnoreCase("MOBILE"))
                           {
                                  if (netInfoCheck.isConnected())
                                  {
                                         haveConnectedMobile = true;
                                  }
                           }
                     }
              }

              return haveConnectedWifi || haveConnectedMobile;
       }

Call Method


if (haveNetworkConnection(context.this))
       {
              //Connection available....
       }
       else
       {
              //No Connection available....
       }


I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)



No comments:

Post a Comment