I tried the answer from this (the accepted answer). I can use the "PING" method but the UI went black since it says it will block the UI Thread. It didn't look good and was disturbing so I tried to use the second method "Connect to a Socket on the Internet" but I don't know how to use the class in Kotlin.
This is the result of converted Java to kotlin by android studio
package com.mockie.daikokuten.helpers import android.os.AsyncTask.execute import android.os.AsyncTask import java.io.IOException import java.net.InetSocketAddress import java.net.Socket internal class InternetCheck(private val mConsumer: Consumer) : AsyncTask<Void, Void, Boolean>() { interface Consumer { fun accept(internet: Boolean?) } init { execute() } override fun doInBackground(vararg voids: Void): Boolean? { try { val sock = Socket() sock.connect(InetSocketAddress("8.8.8.8", 53), 1500) sock.close() return true } catch (e: IOException) { return false } } override fun onPostExecute(internet: Boolean?) { mConsumer.accept(internet) } }
but I DONT KNOW HOW TO USE IT. I tried this way:
InternetCheck{ internet-> Log.d("test", "asdasdas") }
It didnt work and results in an error. It says I have to pass "Consumer".
My question is How to use that class?
Refer to How to Create/Start a New Project in Android Studio, to know how to create an empty activity project. And select Kotlin as the programming language. The main layout of the application contains only one button. Which upon clicking, a toast appears which contains the status of the connectivity.
Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top.
Update: Since Android 10 the use of the NetWorkInfo class and its methods are obsolete, now you must use the ConectivityManager class and getNetworkCapabilities () method from NetworkCapabilities Class.
Kotlin :
fun isOnline(context: Context): Boolean { val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager if (connectivityManager != null) { val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) if (capabilities != null) { if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { Log.i("Internet", "NetworkCapabilities.TRANSPORT_CELLULAR") return true } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { Log.i("Internet", "NetworkCapabilities.TRANSPORT_WIFI") return true } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { Log.i("Internet", "NetworkCapabilities.TRANSPORT_ETHERNET") return true } } } return false }
Java:
public static boolean isOnline(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork()); if (capabilities != null) { if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { Log.i("Internet", "NetworkCapabilities.TRANSPORT_CELLULAR"); return true; } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { Log.i("Internet", "NetworkCapabilities.TRANSPORT_WIFI"); return true; } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { Log.i("Internet", "NetworkCapabilities.TRANSPORT_ETHERNET"); return true; } } } return false; }
both methods require the permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET"/>
Check internet connectivity in android kotlin
fun isNetworkAvailable(context: Context?): Boolean { if (context == null) return false val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) if (capabilities != null) { when { capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> { return true } capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> { return true } capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> { return true } } } } else { val activeNetworkInfo = connectivityManager.activeNetworkInfo if (activeNetworkInfo != null && activeNetworkInfo.isConnected) { return true } } return false }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With