Java Code Examples for android.net.NetworkInfo#isAvailable()

The following examples show how to use android.net.NetworkInfo#isAvailable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceInfo.java    From Cangol-appcore with Apache License 2.0 6 votes vote down vote up
public static int getWifiRssi(Context context) {
    int asu = 85;
    try {
        final NetworkInfo network = ((ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        if (network != null && network.isAvailable() && network.isConnected()) {
            final int type = network.getType();
            if (type == ConnectivityManager.TYPE_WIFI) {
                final WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

                final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                if (wifiInfo != null) {
                    asu = wifiInfo.getRssi();
                }
            }
        }
    } catch (Exception e) {
        Log.e("getWifiRssi", "" + e.getMessage(), e);
    }
    return asu;
}
 
Example 2
Source File: DeviceHelper.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the current network type
 * 
 * @return boolean
 */
public boolean isNetworkTypeWifi() {
	// TODO Auto-generated method stub

	if (checkPermissions("android.permission.INTERNET")) {
		ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cManager.getActiveNetworkInfo();

		if (info != null && info.isAvailable() && info.getTypeName().equals("WIFI")) {
			return true;
		} else {
			Ln.e("error", "Network not wifi");
			return false;
		}
	} else {
		Ln.e(" lost  permission", "lost----> android.permission.INTERNET");
		return false;
	}

}
 
Example 3
Source File: NetworkStauts.java    From WebViewJavaScriptBridge with Apache License 2.0 6 votes vote down vote up
private static int getNetworkClass(Context context) {
    int networkType = NETWORK_TYPE_UNKNOWN;
    try {
        final NetworkInfo network = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        if (network != null && network.isAvailable()
                && network.isConnected()) {
            int type = network.getType();
            if (type == ConnectivityManager.TYPE_WIFI) {
                networkType = NETWORK_TYPE_WIFI;
            } else if (type == ConnectivityManager.TYPE_MOBILE) {
                TelephonyManager telephonyManager = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);
                networkType = telephonyManager.getNetworkType();
            }
        } else {
            networkType = NETWORK_TYPE_UNAVAILABLE;
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return getNetworkClassByType(networkType);
}
 
Example 4
Source File: NetworkUtils.java    From letv with Apache License 2.0 6 votes vote down vote up
public static boolean hasConnect(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivity == null) {
            return false;
        }
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info == null || !info.isAvailable()) {
            return false;
        }
        return true;
    } catch (Throwable e) {
        LogTool.e(TAG, "", e);
        return false;
    }
}
 
Example 5
Source File: NETUtils.java    From GTTools with MIT License 5 votes vote down vote up
/**
 * 获取网络连接类型
 * 
 * @return -1表示没有网络
 */
public static final int getNetWorkType(Context c) {
	ConnectivityManager conn = (ConnectivityManager) c
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (conn == null) {
		return -1;
	}
	NetworkInfo info = conn.getActiveNetworkInfo();
	if (info == null || !info.isAvailable()) {
		return -1;
	}
	int type = info.getType();
	if (type == ConnectivityManager.TYPE_WIFI) {
		return TYPE_WIFI;
	} else {
		TelephonyManager tm = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		switch (tm.getNetworkType()) {
		case TelephonyManager.NETWORK_TYPE_CDMA:
			return TYPE_GPRS;
		case TelephonyManager.NETWORK_TYPE_EDGE:
			return TYPE_GPRS;
		case TelephonyManager.NETWORK_TYPE_GPRS:
			return TYPE_GPRS;
		default:
			return TYPE_FAST;
		}
	}
}
 
Example 6
Source File: NetWorkUtil.java    From LeisureRead with Apache License 2.0 5 votes vote down vote up
public static int getConnectedType() {

    if (LeisureReadApp.getAppContext() != null) {
      ConnectivityManager mConnectivityManager
          = (ConnectivityManager) LeisureReadApp.getAppContext()
          .getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
      if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
        return mNetworkInfo.getType();
      }
    }
    return -1;
  }
 
Example 7
Source File: NetworkUtils.java    From Android-utils with Apache License 2.0 5 votes vote down vote up
@RequiresPermission(ACCESS_NETWORK_STATE)
public static boolean is4G() {
    NetworkInfo info = getActiveNetworkInfo();
    return info != null
            && info.isAvailable()
            && info.getSubtype() == TelephonyManager.NETWORK_TYPE_LTE;
}
 
Example 8
Source File: NetworkUtils.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 判断WIFI网络是否可用
 **/
public static boolean isWifiConnected(Context context) {
    if (context != null) {
        // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        // 获取NetworkInfo对象
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        //判断NetworkInfo对象是否为空 并且类型是否为WIFI
        if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
            return networkInfo.isAvailable();
    }
    return false;
}
 
Example 9
Source File: NetUtils.java    From NetStateBar with Apache License 2.0 5 votes vote down vote up
public static boolean isWifiConnected(Context context) {
	if (context != null) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mWiFiNetworkInfo = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (mWiFiNetworkInfo != null) {
			return mWiFiNetworkInfo.isAvailable();
		}
	}
	return false;
}
 
Example 10
Source File: NetStatusReceiver.java    From zone-sdk with MIT License 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        NetStatue status = NetStatue.NO_WORK;
        if (info != null && info.isAvailable()) {
            if (NetManager.Net_MOBILE.equals(info.getTypeName())) {
                status = NetStatue.MOBILE;
            } else if (NetManager.Net_WIFI.equals(info.getTypeName())) {
                status = NetStatue.WIFI;
            }
        }
        if (listener != null) {
            listener.netWorkChange(status);
        }
    }
}
 
Example 11
Source File: OkUtils.java    From BlueBoard with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前网络连接的类型信息
 */
public static int getConnectedType(Context context) {
    if (context != null) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            return networkInfo.getType();
        }
    }
    return -1;
}
 
Example 12
Source File: OkHttp3Utils.java    From LLApp with Apache License 2.0 5 votes vote down vote up
/**
 * 判断网络是否可用
 *
 * @param context Context对象
 */
public static Boolean isNetworkReachable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo current = cm.getActiveNetworkInfo();
    if (current == null) {
        return false;
    }
    return (current.isAvailable());
}
 
Example 13
Source File: NetWorkUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public boolean isNetWorkAvaliable() {

        if (mConnectivityManager == null) {
            return false;
        }

        NetworkInfo networkinfo = mConnectivityManager.getActiveNetworkInfo();

        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }

        return true;
    }
 
Example 14
Source File: NetworkUtil.java    From NIM_Android_UIKit with MIT License 5 votes vote down vote up
/**
 * 判断是否有网络可用
 *
 * @param context
 * @return
 */
public static boolean isNetAvailable(Context context) {
    NetworkInfo networkInfo = getActiveNetworkInfo(context);
    if (networkInfo != null) {
        return networkInfo.isAvailable();
    } else {
        return false;
    }
}
 
Example 15
Source File: NetUtils.java    From SimplifyReader with Apache License 2.0 5 votes vote down vote up
public static int getConnectedType(Context context) {
	if (context != null) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
			return mNetworkInfo.getType();
		}
	}
	return -1;
}
 
Example 16
Source File: SystemUtil.java    From GankLock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 网络是否可用
 */
public static boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager) MyApplication.getContext()
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getActiveNetworkInfo();
    if (info != null) {
        return info.isAvailable();
    }
    return false;
}
 
Example 17
Source File: NetWorkUtils.java    From ToDoList with Apache License 2.0 5 votes vote down vote up
/**
 * 判断是否有网络连接
 *
 * @param context
 * @return
 */
public static boolean isNetworkConnected(Context context) {
    if (context != null) {
        // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        // 获取NetworkInfo对象
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        //判断NetworkInfo对象是否为空
        if (networkInfo != null)
            return networkInfo.isAvailable();
    }
    return false;
}
 
Example 18
Source File: NetworkUtils.java    From MVVM with MIT License 5 votes vote down vote up
public static boolean isConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable();
        }
    }
    return false;
}
 
Example 19
Source File: SplashScreenActivity.java    From mobikul-standalone-pos with MIT License 4 votes vote down vote up
public void isOnline() {
    ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
    isInternetAvailable = !(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable());
}
 
Example 20
Source File: HWUtils.java    From RePlugin-GameSdk with Apache License 2.0 4 votes vote down vote up
/**
 * 获取网络类型
 * 
 * @param context
 * @return
 */
public static String getNetType(Context context) {
	String ret = "";
	ConnectivityManager mConnectivityManager = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
	if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
		int type = mNetworkInfo.getType();
		if (ConnectivityManager.TYPE_WIFI == type) {
			ret = "wifi";
		} else if (ConnectivityManager.TYPE_MOBILE == type) {
			ret = "3g";
			int subtype = mNetworkInfo.getSubtype();
			switch (subtype) {
			case TelephonyManager.NETWORK_TYPE_GPRS:
			case TelephonyManager.NETWORK_TYPE_EDGE:
			case TelephonyManager.NETWORK_TYPE_CDMA:
			case TelephonyManager.NETWORK_TYPE_1xRTT:
			case TelephonyManager.NETWORK_TYPE_IDEN:
				ret = "2g";
				break;

			case TelephonyManager.NETWORK_TYPE_UMTS:
			case TelephonyManager.NETWORK_TYPE_EVDO_0:
			case TelephonyManager.NETWORK_TYPE_EVDO_A:
			case TelephonyManager.NETWORK_TYPE_HSDPA:
			case TelephonyManager.NETWORK_TYPE_HSUPA:
			case TelephonyManager.NETWORK_TYPE_HSPA:
			case TelephonyManager.NETWORK_TYPE_EVDO_B:
			case TelephonyManager.NETWORK_TYPE_EHRPD:
			case TelephonyManager.NETWORK_TYPE_HSPAP:
				ret = "3g";
				break;

			case TelephonyManager.NETWORK_TYPE_LTE:
				ret = "4g";
				break;

			default:
				ret = "未知";
				break;
			}

		} else {
			ret = "未知";
		}
	}
	return ret;
}