Last active
October 29, 2019 02:53
-
-
Save ndangnguyen/260baa9ab2a6eddf8a99c7d242a39700 to your computer and use it in GitHub Desktop.
This file contains 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
public static boolean isNetworkConnected() { | |
final ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
if (cm != null) { | |
if (Build.VERSION.SDK_INT < 23) { | |
final NetworkInfo ni = cm.getActiveNetworkInfo(); | |
if (ni != null) { | |
return (ni.isConnected() && (ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_MOBILE)); | |
} | |
} else { | |
final Network n = cm.getActiveNetwork(); | |
if (n != null) { | |
final NetworkCapabilities nc = cm.getNetworkCapabilities(n); | |
return (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)); | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment