Skip to content

Instantly share code, notes, and snippets.

@pfieffer
Last active November 1, 2019 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfieffer/dbd0557f5306d05afdaeec8f015b8b0c to your computer and use it in GitHub Desktop.
Save pfieffer/dbd0557f5306d05afdaeec8f015b8b0c to your computer and use it in GitHub Desktop.
To find if the Android Device is connected to the internet or not
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkHelper {
public static final boolean hasNetworkAccess(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnectedOrConnecting();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment