Example usage for android.net ConnectivityManager getNetworkInfo

List of usage examples for android.net ConnectivityManager getNetworkInfo

Introduction

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

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getNetworkInfo(@Nullable Network network) 

Source Link

Document

Returns connection status information about a particular Network.

Usage

From source file:com.rincliu.library.util.RLNetUtil.java

/**
 * @param context/*from w  ww  .j a v  a  2 s  .co  m*/
 * @return
 * @throws Exception
 */
public static boolean isWifiDataEnable(Context context) throws Exception {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean isWifiDataEnable = false;
    isWifiDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnectedOrConnecting();
    return isWifiDataEnable;
}

From source file:com.rincliu.library.util.RLNetUtil.java

/**
 * @param context//from   w ww .j a v a 2  s.co m
 * @return
 * @throws Exception
 */
public static boolean isMobileDataEnable(Context context) throws Exception {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean isMobileDataEnable = false;
    isMobileDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnectedOrConnecting();
    return isMobileDataEnable;
}

From source file:cm.aptoide.pt.util.NetworkUtils.java

public static boolean isConnectionAvailable(Context context) {
    ConnectivityManager connectivityState = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean connectionAvailable = false;
    try {//from  w  w w  .ja va2  s .  c  o m
        connectionAvailable = connectivityState.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED;
        Log.d("ManagerDownloads", "isConnectionAvailable mobile: " + connectionAvailable);
    } catch (Exception e) {
    }
    try {
        connectionAvailable = connectionAvailable
                || connectivityState.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED;
        Log.d("ManagerDownloads", "isConnectionAvailable wifi: " + connectionAvailable);
    } catch (Exception e) {
    }
    try {
        connectionAvailable = connectionAvailable
                || connectivityState.getNetworkInfo(6).getState() == NetworkInfo.State.CONNECTED;
        Log.d("ManagerDownloads", "isConnectionAvailable wimax: " + connectionAvailable);
    } catch (Exception e) {
    }
    try {
        connectionAvailable = connectionAvailable
                || connectivityState.getNetworkInfo(9).getState() == NetworkInfo.State.CONNECTED;
        Log.d("ManagerDownloads", "isConnectionAvailable ethernet: " + connectionAvailable);
    } catch (Exception e) {
    }

    return connectionAvailable;
}

From source file:net.peterkuterna.android.apps.devoxxsched.service.SyncService.java

/**
 * Are we connected to a WiFi network?/*w w  w . j a  v a2 s.co  m*/
 */
private static boolean isWifiConnected(Context context) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager != null) {
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return (networkInfo != null && networkInfo.getState().equals(State.CONNECTED));
    }

    return false;
}

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:cw.kop.autobackground.files.FileHandler.java

public static boolean download(Context appContext) {

    if (!isDownloading) {
        ConnectivityManager connect = (ConnectivityManager) appContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifi = connect.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi != null && wifi.isConnected() && AppSettings.useWifi()) {
        } else if (mobile != null && mobile.isConnected() && AppSettings.useMobile()) {
        } else {/*from w  ww  . j  av a  2s.  c  o m*/
            if (AppSettings.useToast()) {
                Toast.makeText(appContext, "No connection available,\ncheck Download Settings",
                        Toast.LENGTH_SHORT).show();
            }

            Intent resetDownloadIntent = new Intent(FileHandler.DOWNLOAD_TERMINATED);
            LocalBroadcastManager.getInstance(appContext).sendBroadcast(resetDownloadIntent);
            return false;
        }
        isDownloading = true;
        downloadThread = new DownloadThread(appContext);
        downloadThread.start();
        if (AppSettings.useToast()) {
            Toast.makeText(appContext, "Downloading images", Toast.LENGTH_SHORT).show();
        }
        return true;
    } else {
        return false;
    }
}

From source file:uk.ac.ucl.excites.sapelli.shared.util.android.DeviceControl.java

private static boolean isOnline(Context context, Integer networkType) {
    try {//  w ww  .jav  a 2s.co  m
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = networkType != null ? cm.getNetworkInfo(networkType) : cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnected();
    } catch (Exception e) {
        Log.e(DeviceControl.class.getSimpleName(), "isOnline()", e);
    }
    return false;
}

From source file:com.ryan.ryanreader.common.General.java

/**
 * //ww w  .  jav  a  2s  .c o m
 * ?WIFI
 * 
 * @param context
 * @return
 */
public static boolean isConnectionWifi(final Context context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    return info != null && info.isConnected();
}

From source file:com.secupwn.aimsicd.utils.Helpers.java

/**
 * Checks if Network connectivity is available to download OpenCellID data
 * Requires:        android:name="android.permission.ACCESS_NETWORK_STATE"
 *//*from  w  w w.j a  v  a2  s .  c  o  m*/
public static Boolean isNetAvailable(Context context) {
    try {
        ConnectivityManager cM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = cM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileInfo = cM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifiInfo != null && mobileInfo != null) {
            return wifiInfo.isConnected() || mobileInfo.isConnected();
        }
    } catch (Exception e) {
        log.error(e.getMessage(), 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();
}