Created
July 3, 2015 11:49
-
-
Save thuytrinh/eea47e9cb3fb575714b8 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
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
/** | |
* NOTE: Please remove this class if we decide to integrate with RoboSpice. | |
* RoboSpice does implement a DefaultNetworkStateChecker class which takes care of everything NetworkUtils does. | |
*/ | |
public class NetworkUtils { | |
public static boolean isNetworkAvailable(Context context) { | |
ConnectivityManager connectivityManager = getConnectivityManager(context); | |
NetworkInfo[] allNetworkInfo = connectivityManager.getAllNetworkInfo(); | |
if (allNetworkInfo != null) | |
for (NetworkInfo networkInfo : allNetworkInfo) { | |
NetworkInfo.State state = networkInfo.getState(); | |
if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) | |
return true; | |
} | |
return false; | |
} | |
public static boolean isWiFiAvailable(Context context) { | |
ConnectivityManager connectivityManager = getConnectivityManager(context); | |
NetworkInfo[] allNetworkInfo = connectivityManager.getAllNetworkInfo(); | |
if (allNetworkInfo != null) | |
for (NetworkInfo networkInfo : allNetworkInfo) | |
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) | |
if (networkInfo.isConnectedOrConnecting()) | |
return true; | |
return false; | |
} | |
static ConnectivityManager getConnectivityManager(Context context) { | |
return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment