Last active
August 15, 2017 13:51
-
-
Save ardianta/319a5804d4c4c8341a469d99be3db881 to your computer and use it in GitHub Desktop.
Method to check android internet connection
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
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<uses-permission android:name="android.permission.INTERNET" /> |
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 isOnline(Context context) { | |
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); | |
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected(); | |
} | |
public static boolean isConnectedWifi(Context context) { | |
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); | |
return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI; | |
} | |
public static boolean isConnectedMobile(Context context) { | |
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); | |
return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE; | |
} | |
// ref: https://stackoverflow.com/a/41431609/2658551 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment