Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check internet connectivity android in kotlin

Tags:

android

kotlin

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?

like image 537
Kakashi Avatar asked Jul 02 '18 18:07

Kakashi


People also ask

How do I check my Internet connection Kotlin?

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.

How do I check my internet connection status?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top.


2 Answers

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.

How to check connectivity in Android?

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"/> 
like image 88
Jorgesys Avatar answered Sep 24 '22 10:09

Jorgesys


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 } 
like image 39
Aftab Alam Avatar answered Sep 26 '22 10:09

Aftab Alam