Reputation: 47
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.
I try this code but I don't understand where I have to put it and I get
'TYPE_WIFI, TYPE_MOBILE, TYPE_ETHERNET: Int' is deprecated. Deprecated in Java
private fun isInternetAvailable(context: Context): Boolean {
var result = false
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val actNw =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
result = when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
} else {
connectivityManager.run {
connectivityManager.activeNetworkInfo?.run {
result = when (type) {
ConnectivityManager.TYPE_WIFI -> true
ConnectivityManager.TYPE_MOBILE -> true
ConnectivityManager.TYPE_ETHERNET -> true
else -> false
}
}
}
}
return result
}
Someone can explain me how to make this check?
Upvotes: 3
Views: 5352
Reputation: 71
You have to extend AndroidViewModel()
class. After that you can reach application context in your viewModel
.
class viewModel(app: Application): AndroidViewModel(app) {}
Upvotes: 1
Reputation: 1497
I created a helper class. Network.kt
object Network {
private const val NETWORK_STATUS_NOT_CONNECTED = 0
private const val NETWORK_STATUS_WIFI = 1
private const val NETWORK_STATUS_MOBILE = 2
private const val TYPE_WIFI = 1
private const val TYPE_MOBILE = 2
private const val TYPE_NOT_CONNECTED = 0
private fun connectivityStatus(context: Context): Int {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = connectivityManager.activeNetworkInfo
if (null != activeNetwork) {
if (activeNetwork.type == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI
if (activeNetwork.type == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE
}
return TYPE_NOT_CONNECTED
}
private fun connectivityStatusString(context: Context): Int {
val connection = connectivityStatus(context)
var status = -1
if (connection == TYPE_WIFI) status = NETWORK_STATUS_WIFI else if (connection == TYPE_MOBILE) status = NETWORK_STATUS_MOBILE else if (connection == TYPE_NOT_CONNECTED) status = NETWORK_STATUS_NOT_CONNECTED
return status
}
fun checkConnectivity(context : Context):Boolean{
val status = connectivityStatusString(context)
return status == NETWORK_STATUS_WIFI || status == NETWORK_STATUS_MOBILE
}
}
and to access it you need to use it like this
if (Network.checkConnectivity(this@MainActivity))
\\Internet is working
else
\\No internet connectivity
Upvotes: 3