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 isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            if (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }/*from w w  w .ja va  2 s .  c  om*/
        }
    }
    return false;
}

From source file:Main.java

public static boolean isConnection(Context applicationContext) {

    ConnectivityManager connectivityManager = (ConnectivityManager) applicationContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
        return true;
    }// w w  w . j  a v a 2s .  c o  m

    return false;
}

From source file:Main.java

/**
 * @param context/*from   w w w.j a v  a2 s.  c o m*/
 * @return
 */
public static String getNetWorkName(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return "MOBILE";
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null)
        return info.getTypeName();
    else
        return "MOBILE";
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mWiFiNetworkInfo != null && mWiFiNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return mWiFiNetworkInfo.isAvailable();
        }//ww  w  . j  av  a 2s  .  co  m
    }
    return false;
}

From source file:Main.java

public static boolean isMobile(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        return true;
    }//from  w  w w .ja  v  a2s .com
    return false;
}

From source file:Main.java

public static boolean isNetworkAvailable(final Context context) {
    if (context != null) {
        final ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo info = connManager.getActiveNetworkInfo();
        return info != null && info.isConnected();
    }//w  w w  . j  a v  a2 s.co  m
    return false;
}

From source file:Main.java

public static boolean isNetConnected(Context context) {
    boolean ret = false;
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo == null) {
        return ret;
    }//from w w w  . ja v a2s .c  o  m
    ret = networkInfo.isAvailable() & networkInfo.isConnected();
    return ret;
}

From source file:Main.java

/**
 * get mobile NetWork Type/*from  w  w  w  .ja  v a 2s  . co m*/
 * 
 * @param context
 * @return
 */
public static String getNetWorkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo workinfo = connectivityManager.getActiveNetworkInfo();
    if (workinfo != null) {
        if (workinfo.getType() == ConnectivityManager.TYPE_BLUETOOTH) {
            return "b";
        }
        if (workinfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return "m";
        }
        if (workinfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return "w";
        }
    }
    return null;
}

From source file:Main.java

public static boolean isNetWorkAvalible(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return false;
    }/*w ww  . jav a  2  s. co  m*/
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni == null || !ni.isAvailable()) {
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * @param context//from w w  w.  jav a2 s .  co  m
 * @return true if the device has an internet connection
 */
public final static boolean hasInternetConnection(Context context) {
    if (PackageManager.PERMISSION_GRANTED == context
            .checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)) {
        final ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            return networkInfo.isConnected();
        }
    }
    return false;
}