標籤:android 網路連接
/** * 判斷移動網路是否開啟 * * @param context * @return */public static boolean isNetEnabled(Context context) {TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);if (tm != null) {if (tm.getNetworkType() != TelephonyManager.NETWORK_TYPE_UNKNOWN) {Log.i("sjf", "移動網路已經開啟");return true;}}Log.i("sjf", "移動網路還未開啟");return false;}/** * 判斷WIFI網路是否開啟 * * @param context * @return */public static boolean isWifiEnabled(Context context) {WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);if (wm != null && wm.isWifiEnabled()) {Log.i("sjf", "Wifi網路已經開啟");return true;}Log.i("sjf", "Wifi網路還未開啟");return false;}/** * 判斷移動網路是否串連成功 * * @param context * @return */public static boolean isNetContected(Context context) {ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);if (cm != null && info != null && info.isConnected()) {Log.i("sjf", "移動網路連接成功");return true;}Log.i("sjf", "移動網路連接失敗");return false;}/** * 判斷WIFI是否串連成功 * * @param context * @return */public static boolean isWifiContected(Context context) {ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);if (info != null && info.isConnected()) {Log.i("sjf", "Wifi網路連接成功");return true;}Log.i("sjf", "Wifi網路連接失敗");return false;}/** * 判斷移動網路和WIFI是否開啟 * * @param context * @return */public static boolean isNetWorkEnabled(Context context) {return (isNetEnabled(context) || isWifiEnabled(context));}/** * 判斷移動網路和WIFI是否串連成功 * @param context * @return */public static boolean isNetworkConnected(Context context) {return (isWifiContected(context) || isNetContected(context));}
[Android]判斷網路連接是否可用