title: Android check internet connection in kotlin
body: <p>I need to check if the device have internet connection, I search for some examples but when i copy and paste the code i always get errors or deprecated function. I also not understand where I have to put the method that check the connection, because I need to check the internet connection in the viewModel to make some request, and all the methods that i found have Context in the parameters, and I can't get Context in viewModel.</p>
  <p>I try this code but I don't understand where I have to put it and I get</p>
  <p><code>'TYPE_WIFI, TYPE_MOBILE, TYPE_ETHERNET: Int' is deprecated. Deprecated in Java</code></p>
  <pre><code>private fun isInternetAvailable(context: Context): Boolean {
          var result = false
          val connectivityManager =
              context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
              val networkCapabilities = connectivityManager.activeNetwork ?: return false
              val actNw =
                  connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
              result = when {
                  actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -&gt; true
                  actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -&gt; true
                  actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -&gt; true
                  else -&gt; false
              }
          } else {
              connectivityManager.run {
                  connectivityManager.activeNetworkInfo?.run {
                      result = when (type) {
                          ConnectivityManager.TYPE_WIFI -&gt; true
                          ConnectivityManager.TYPE_MOBILE -&gt; true
                          ConnectivityManager.TYPE_ETHERNET -&gt; true
                          else -&gt; false
                      }
  
                  }
              }
          }
  
          return result
      }
  </code></pre>
  <p>Someone can explain me how to make this check?</p>