I have the url to connect but I need to verify to know in what way I can say when there is connection or not through a toast.Greetings and thanks in advance
I have the url to connect but I need to verify to know in what way I can say when there is connection or not through a toast.Greetings and thanks in advance
You can check the connection status using the ConnectivityManager
:
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo infoRed = cm.getActiveNetworkInfo();
boolean conectado = infoRed.isConnectedOrConnecting();
To discriminate between Wifi or data connection you can consult:
int tipo = activeNetwork.getType();
switch(tipo){
case ConnectivityManager.TYPE_WIFI:
// caso wifi
break;
case ConnectivityManager.TYPE_MOBILE:
// caso telefono datos moviles
break;
}
More types are found in the ConnectivityManager
documentation.
There is documentation on the subject in Spanish at developer.android.com .
Then you create a Toast
or a Snackbar
with your information:
int conectadoEstado = (conectado) ? R.string.connectado : R.string.no_conectado;
Snackbar.make(view, connectadoEstado, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
int conectadoEstado = (conectado) ? R.string.connectado : R.string.no_conectado;
Toast.makeText(context, conectadoEstado, Toast.LENGTH_SHORT).show();
You can also register a BroadcastReceiver
listening by "android.net.conn.CONNECTIVITY_CHANGE", and use ConnectivityManager
to determine the exact state of connectivity.