Skip to content

Instantly share code, notes, and snippets.

@karthikraj-duraisamy
Created October 27, 2017 18:34
Show Gist options
  • Save karthikraj-duraisamy/92b5af1eaa6fe031434fc55846e89942 to your computer and use it in GitHub Desktop.
Save karthikraj-duraisamy/92b5af1eaa6fe031434fc55846e89942 to your computer and use it in GitHub Desktop.
This class is useful to perform connection availability check in Android applications.
import android.Manifest;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.RequiresPermission;
public class InternetUtils {
private static ConnectivityManager connectivityManager;
private static ConnectivityManager getConnectivityManager(Context context) {
if (connectivityManager == null)
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return connectivityManager;
}
/**
* Let's know whether internet is connected or not.
* @param context
* @return true if it is connected to internet, false otherwise
*/
@RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE)
public static boolean isConnected(Context context) {
NetworkInfo networkInfo = getConnectivityManager(context).getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected());
}
/**
* Return the type of the network
* @param context
* @return type of the network like "WIFI" or "MOBILE" if is connected. If it not connected then this method will return null
*/
@RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE)
public static String getConnectionType(Context context) {
return isConnected(context) ? getConnectivityManager(context).getActiveNetworkInfo().getTypeName() : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment