Skip to content

Instantly share code, notes, and snippets.

@thuytrinh
Created July 3, 2015 11:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save thuytrinh/eea47e9cb3fb575714b8 to your computer and use it in GitHub Desktop.
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