Skip to content

Instantly share code, notes, and snippets.

@PepeuCps
Created July 15, 2014 16:38
Show Gist options
  • Save PepeuCps/fb0b794c201cc8b77ee6 to your computer and use it in GitHub Desktop.
Save PepeuCps/fb0b794c201cc8b77ee6 to your computer and use it in GitHub Desktop.
Verifica se o device está conectado na internet (3G ou WIFI).
public static boolean hasConnection(Context context) {
try {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()) {
Log.i("hasConnection","Status de conexao 3G: " + cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected());
return true;
} else if(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()){
Log.i("hasConnection","Status de conexao Wifi: " + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected());
return true;
} else {
Log.i("hasConnection","Status de conexao Wifi: " + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected());
Log.i("hasConnection","Status de conexao 3G: " + cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected());
return false;
}
} catch (Exception e) {
Log.e("hasConnection", "Exception hasConnection " + e.getMessage());
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment