Example usage for android.net ConnectivityManager getActiveNetworkInfo

List of usage examples for android.net ConnectivityManager getActiveNetworkInfo

Introduction

In this page you can find the example usage for android.net ConnectivityManager getActiveNetworkInfo.

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getActiveNetworkInfo() 

Source Link

Document

Returns details about the currently active default data network.

Usage

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    try {//from  w ww  . ja  va 2  s  . co  m
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkPresent(Context context) {
    boolean isNetworkAvailable = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {/* ww  w .  j  a v a  2  s .  co  m*/

        if (cm != null) {
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null) {

                isNetworkAvailable = netInfo.isConnected();
                //                    Toast.makeText(context, "Connecting...", Toast.LENGTH_SHORT).show();
                //Log.d("NETWORK<<","Connecting...."+netInfo.getReason());
            }
        }
    } catch (Exception ex) {
        //Log.e("Network Avail Error", ex.getMessage());

    }
    //        check for wifi also
    if (!isNetworkAvailable) {

        WifiManager connec = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        State wifi = cm.getNetworkInfo(1).getState();
        if (connec.isWifiEnabled() && wifi.toString().equalsIgnoreCase("CONNECTED")) {
            isNetworkAvailable = true;
            //Log.d("WIFI NETWORK<<","WIFI connected");
        } else {

            isNetworkAvailable = false;
            // Log.d("WIFI Network<<","WIFI not connected");
        }

    }
    return isNetworkAvailable;

}

From source file:Main.java

public static boolean isNetworkConncected(Context context) {
    ConnectivityManager connectiveManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectiveManager == null) {
        return false;
    }//from w  w w. j a v a2  s . c o m
    NetworkInfo networkInfo = connectiveManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        return networkInfo.isAvailable();
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager manager = (ConnectivityManager) ctx.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }/* w  ww  .ja va  2  s.  c  om*/
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean checkNet(Activity act) {
    ConnectivityManager manager = (ConnectivityManager) act.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }//from   www .  j a  va2  s. c o  m
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean CheckNet(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }//from w  w  w .ja  v  a  2  s.  c om
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    } else {//from ww  w  . j  a v a  2  s.  c o m
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info == null) {
            return false;
        } else {
            if (info.isAvailable()) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static Boolean isNetworkConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }/* w  w  w. ja v a2s  . co  m*/
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean hasNetwork(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }/* w  ww  . j a va2 s  . co m*/
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        return false;
    }
    return true;

}

From source file:com.codeskraps.lolo.misc.Utils.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected())
        return true;
    return false;
}