Después de consultar con expertos en el tema, programadores de deferentes áreas y maestros dimos con la respuesta a la interrogande y la plasmamos en esta publicación.
Solución:
Actualizar:
Desde Android 10 el uso de la clase NetWorkInfo y sus métodos está obsoleto, ahora debes usar la clase ConectivityManager y el método getNetworkCapabilities() de NetworkCapabilities Class.
¿Cómo comprobar la conectividad en 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;
ambos métodos requieren los permisos:
Comprobar la conectividad a internet en 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
fun verifyAvailableNetwork(activity:AppCompatActivity):Boolean
val connectivityManager=activity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo=connectivityManager.activeNetworkInfo
return networkInfo!=null && networkInfo.isConnected
Valoraciones y reseñas
No se te olvide dar visibilidad a este ensayo si te fue de ayuda.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)