Android Utililty Methods Network State Check

List of utility methods to do Network State Check

Description

The list of methods to do Network State Check are organized into topic(s).

Method

intgetNetworkState(Context context)
get Network State
if (null != context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    State state;
    NetworkInfo networkInfo;
    if (null != connManager) {
        networkInfo = connManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
...
booleanisLocationEnabledNetwork(Context c)
is Location Enabled Network
LocationManager lm = (LocationManager) c
        .getSystemService(Context.LOCATION_SERVICE);
return lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
booleanisOpenNetwork(Context context)
is Open Network
ConnectivityManager connManager = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
if (connManager.getActiveNetworkInfo() != null) {
    return connManager.getActiveNetworkInfo().isAvailable();
return false;
BitmaploadImageFromNetwork(String photoUrl)
load Image From Network
Bitmap bm = null;
try {
    InputStream in = new java.net.URL(photoUrl).openStream();
    bm = BitmapFactory.decodeStream(in);
} catch (Exception e) {
    Log.e("Error", e.getMessage());
    e.printStackTrace();
return bm;
booleanIsInternetReachable()
Is Internet Reachable
try {
    InetAddress.getByName("www.google.com").isReachable(3);
    return true;
} catch (UnknownHostException e) {
    return false;
} catch (IOException e) {
    return false;
voidsetMobileDataEnabled(Activity a, Context context, boolean enabled)
set Mobile Data Enabled
final ConnectivityManager conman = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
Class conmanClass = null;
try {
    conmanClass = Class.forName(conman.getClass().getName());
} catch (ClassNotFoundException e) {
Field iConnectivityManagerField = null;
...
booleanisNetworkAvailable(Activity mainActivity)
is Network Available
ConnectivityManager connectivityManager = (ConnectivityManager) mainActivity
        .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
        .getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
booleanisDeviceOnline(Activity activity)
Iterates all network infos and returns true if one of them is available for sending data.
ConnectivityManager cm = (ConnectivityManager) activity
        .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
if (netInfo != null) {
    for (int i = 0; i < netInfo.length; i++) {
        if (netInfo[i].isConnected())
            return true;
return false;
booleanisConnected(final Activity act)
is Connected
final ConnectivityManager connMgr = (ConnectivityManager) act
        .getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
    return true;
return false;
booleanisOnline(Activity activity)
is Online
ConnectivityManager cm = (ConnectivityManager) activity
        .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
    return true;
return false;