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:Main.java

public static boolean ifWifiConnected(Context context) {
    ConnectivityManager connectionManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiCheck = connectionManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return wifiCheck.isConnected();
}

From source file:Main.java

/**
 * Check if device is connected to some network.
 * @param context Context.// w  w w . j a  v a2  s . c  o  m
 * @return True if device is connected, False if not.
 */
public static boolean checkConnection(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (mWifi.isConnected())
        return true;
    else
        return false;
}

From source file:Main.java

private static boolean checkNetWork_b(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
            .getSystemService(context.CONNECTIVITY_SERVICE);
    NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mNetworkInfo = connectMgr.getActiveNetworkInfo();
    boolean bmob = mobNetInfo.isConnected();
    boolean bwifi = wifiNetInfo.isConnected();
    if (!bmob && !bwifi) {
        // Log.i(TAG, "unconnect");
        // unconnect network
        return false;
    } else {/*from   w  w  w  .  ja  v a  2  s  .  c o m*/
        // connect network
        return true;
    }

}

From source file:Main.java

static public synchronized boolean checkConnectivity(Context context) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    // mMobile = prefs.getBoolean(ENUMPrefs.ENUM_PREF_MOBILE, false);
    mOnline = false;//www  .  ja  va 2 s .c  o m

    ConnectivityManager mCmgr = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);

    NetworkInfo ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (ni != null && ni.isConnected()) { /* wifi is on */
        mOnline = true;
    } else {
        if (mMobile) {
            /* if mobile is active and EDGE or better, we're good */
            ni = mCmgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (ni != null && ni.isConnected()) {
                mOnline = (ni.getSubtype() >= TelephonyManager.NETWORK_TYPE_EDGE);
            }
        }
    }
    return mOnline;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifi.isAvailable())
        return true;
    else//from w  ww .ja va 2 s  . c  o  m
        return false;
}

From source file:Main.java

public static String checkWifiConnection(final Activity activity) throws Exception {
    ConnectivityManager connManager = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (!mWifi.isConnected()) {
        new AlertDialog.Builder(activity).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Error")
                .setMessage("Wifi not connected. Application will exit.")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override/*from   w w w.  j a  va  2  s . co m*/
                    public void onClick(DialogInterface dialog, int which) {
                        //activity.finish();
                    }
                }).show();
        throw new Exception("Wifi");
    }
    WifiManager wifiMgr = (WifiManager) activity.getSystemService(Activity.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
}

From source file:Main.java

public static boolean isConnectionToWifi(Context applicationContext) {
    ConnectivityManager connectivityManager = (ConnectivityManager) applicationContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

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

From source file:Main.java

public static boolean isConnectionToMobile(Context applicationContext) {
    ConnectivityManager connectivityManager = (ConnectivityManager) applicationContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
        return true;
    }/*ww w .j a  v  a  2  s.  c  om*/
    return false;
}

From source file:Main.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (mgr == null)
        return false;
    NetworkInfo info = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info == null)
        return false;
    NetworkInfo.State state = info.getState();
    return state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING;
}

From source file:Main.java

public static boolean isWIFIConnected(Context context) {
    ConnectivityManager cmg = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean result = true;
    NetworkInfo active = cmg.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (active == null || !active.isConnected()) {
        result = false;// w ww.  j  ava 2 s.  c  o m
    }
    return result;
}