Last active
November 1, 2019 06:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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