Example usage for android.net NetworkInfo isConnected

List of usage examples for android.net NetworkInfo isConnected

Introduction

In this page you can find the example usage for android.net NetworkInfo isConnected.

Prototype

@Deprecated
public boolean isConnected() 

Source Link

Document

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Usage

From source file:com.mytwitter.Network.NetworkHelper.java

/**
 * Checks that a route to the remote host exists.
 *//*from   ww w .j  a va 2  s.co m*/
public static boolean isHostRoutable(Context context, String hostname) {

    boolean result = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();

    if (connectivity != null && networkInfo != null && networkInfo.isConnected()) {
        int networkType = networkInfo.getType();
        int ipAddress = lookupHost(hostname);
        result = connectivity.requestRouteToHost(networkType, ipAddress);
    }

    return result;
}

From source file:Main.java

public static boolean CheckNetworkConnect(Context context) {
    boolean result = false;
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo ethInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
    NetworkInfo activeInfo = manager.getActiveNetworkInfo();
    Log.v("networkInfo", "mobile:" + mobileInfo.isConnected() + "\n" + "wifi:" + wifiInfo.isConnected() + "\n"
            + "eth:" + ethInfo.isConnected() + "\n" + "active:" + activeInfo.getTypeName());
    if (wifiInfo.isConnected() || ethInfo.isConnected()) {
        result = true;//from   w ww . j a  va 2  s.  c  o  m
    }
    return result;
}

From source file:com.nextgis.metroaccess.MetaDownloader.java

static boolean IsNetworkAvailible(Context c) {
    ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);

    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null) {
        int netType = info.getType();
        if (netType == ConnectivityManager.TYPE_WIFI) {
            return info.isConnected();
        } else if (netType == ConnectivityManager.TYPE_MOBILE && !tm.isNetworkRoaming()) {
            return info.isConnected();
        }/*from   w w w  .jav  a  2 s  .co  m*/
    }
    return false;
}

From source file:com.example.admin.processingboilerplate.MainActivity.java

public static boolean isConnectedWifi() {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm == null ? null : cm.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected()
            && networkInfo.getType() == ConnectivityManager.TYPE_WIFI);
}

From source file:Main.java

public static boolean isNetworkAvailable(Context ctx) {
    int networkStatePermission = ctx.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);

    if (networkStatePermission == PackageManager.PERMISSION_GRANTED) {

        ConnectivityManager mConnectivity = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        // Skip if no connection, or background data disabled
        NetworkInfo info = mConnectivity.getActiveNetworkInfo();
        if (info == null) {
            return false;
        }/*from w w  w .j  ava  2  s  .  c o  m*/
        // Only update if WiFi
        int netType = info.getType();
        // int netSubtype = info.getSubtype();
        if ((netType == ConnectivityManager.TYPE_WIFI) || (netType == ConnectivityManager.TYPE_MOBILE)) {
            return info.isConnected();
        } else {
            return false;
        }
    } else {
        return true;
    }
}

From source file:net.kourlas.voipms_sms.Utils.java

/**
 * Checks if the Internet connection is available.
 *
 * @param applicationContext The application context.
 * @return true if the Internet connection is available, false otherwise.
 *///from   ww  w  .ja  v a  2  s. c  o m
public static boolean isNetworkConnectionAvailable(Context applicationContext) {
    ConnectivityManager connMgr = (ConnectivityManager) applicationContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected();
}

From source file:com.dmsl.anyplace.utils.NetworkUtils.java

public static boolean isOnlineWiFiOrMobile(Context context) {
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isWifiConn = networkInfo.isConnected();
    networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    boolean isMobileConn = networkInfo.isConnected();
    return isMobileConn || isWifiConn;
}

From source file:io.lqd.sdk.model.LQDevice.java

private static String getInternetConnectivity(Context context) {
    if (LiquidTools.checkForPermission(permission.ACCESS_NETWORK_STATE, context)) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mNetwork = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mWifi != null && mWifi.isConnected()) {
            return "WiFi";
        } else if (mNetwork != null && mNetwork.isConnected()) {
            return "Cellular";
        } else {/*from   w w  w  . j a va 2s.com*/
            return "No Connectivity";
        }
    } else {
        return "No ACCESS_NETWORK_STATE permission";
    }
}

From source file:longism.com.api.APIUtils.java

/**
 * TODO Function: check network connected,
 *
 * @param activity/*from   ww w .  j a  va 2  s.  c  o  m*/
 * @return true if Network is connected
 * @require: android.Manifest.permission.ACCESS_NETWORK_STATE
 * @author: Nguyen Long
 * @date: July 08, 2016
 */
public static boolean isNetworkConnected(Activity activity) {
    try {
        ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            return true;
        }
        return false;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return mWifi != null && mWifi.isConnected();
}