¿Cómo verificar la conexión a Internet para cada procesamiento en Android?

In my application, most of the processing in all Activity need to connect to Server to get data.
So, where should I check the Internet connection?
Is there any methods that can tell me when the connection is unavailable?
Finally, how to implement it? Is there any API for handle this case?

preguntado el 24 de agosto de 12 a las 03:08

2 Respuestas

// just checks if network connectivity is available 
public boolean isNetworkConnected(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) return true;
    return false;           
}//end method

Respondido 24 ago 12, 04:08

so, where should I call this and what can I do if connection return false? - jjlin

that's a whole new question...you call this when you need it, if it returns false you know there's no connection... - slinden77

utilizar el ConnectivityManager clase y Context.getSystemService(Context.CONNECTIVITY_SERVICE) method. More information: http://developer.android.com/reference/android/net/ConnectivityManager.html

Respondido 24 ago 12, 03:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.