Publicado en Android

Resolver deprecated: ConnectivityManager.activeNetworkInfo


Si usamos la API 29 para compilar nuestra apps y se usa NetworkInfo y el Android Studio lo marca como Deprecated, aquí la solución

@RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE)
private fun isNetworkAvailable(context: Context): Boolean {
    val connectivityManager =
        context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        val activeNetwork = connectivityManager.activeNetwork ?: return false
        val networkCapabilities =
            connectivityManager.getNetworkCapabilities(activeNetwork) ?: return false
        return when {
            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true
            networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
            else -> false
        }
    } else {
        val activeNetwork = connectivityManager.activeNetworkInfo ?: return false
        return activeNetwork.isConnectedOrConnecting
    }
}
Publicado en Android

Solución deprecated ViewModelProviders AndroidX Kotlin


Al actualizar las dependencias de lifecycle

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

La declaración del viewmodel de los ejemplos de Android Studio marcan dreprecated el uso de ViewModelProviders

Sigue leyendo “Solución deprecated ViewModelProviders AndroidX Kotlin”