Java Code Examples for android.net.ConnectivityManager#getAllNetworkInfo()

The following examples show how to use android.net.ConnectivityManager#getAllNetworkInfo() . 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: NetworkConnUtil.java    From RairDemo with Apache License 2.0 6 votes vote down vote up
/**
 * WiFi是否有效
 *
 * @param context
 * @return
 */
public static boolean isWiFiActive(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        //NetworkInfo infos = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getTypeName().equalsIgnoreCase("WIFI") && info[i].isConnected()) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 2
Source File: SelectLocationTabsActivity.java    From MuslimMateAndroid with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isInternetOn(Context context) {
    boolean isMobile = false, isWifi = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] infoAvailableNetworks = cm.getAllNetworkInfo();

    if (infoAvailableNetworks != null) {
        for (NetworkInfo network : infoAvailableNetworks) {

            if (network.getType() == ConnectivityManager.TYPE_WIFI) {
                if (network.isConnected() && network.isAvailable())
                    isWifi = true;
            }
            if (network.getType() == ConnectivityManager.TYPE_MOBILE) {
                if (network.isConnected() && network.isAvailable())
                    isMobile = true;
            }
        }
    }

    return isMobile || isWifi;

}
 
Example 3
Source File: MxUtils.java    From MxVideoPlayer with Apache License 2.0 6 votes vote down vote up
static boolean isNetworkConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.
            getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager == null) {
        return false;
    } else {
        NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

        if (networkInfo != null && networkInfo.length > 0) {
            for (NetworkInfo aNetworkInfo : networkInfo) {
                if (aNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 4
Source File: NetUtil.java    From likequanmintv with Apache License 2.0 6 votes vote down vote up
public static boolean isNetWorkConnectted(){
    ConnectivityManager connectivity = (ConnectivityManager) APP.getContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info!=null&&info.isConnected()) {
            return true;
        }else{
            NetworkInfo[] allNetworkInfo = connectivity.getAllNetworkInfo();
            if (allNetworkInfo != null) {
                int length = allNetworkInfo.length;
                for (int i = 0; i < length; i++) {
                    // 判断获得的网络状态是否是处于连接状态
                    if (allNetworkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
            return false;

        }
    }
    return false;
}
 
Example 5
Source File: TDevice.java    From AndroidReview with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isWifiOpen() {
    boolean isWifiConnect = false;
    ConnectivityManager cm = (ConnectivityManager) BaseApplication
            .context().getSystemService(Context.CONNECTIVITY_SERVICE);
    // check the networkInfos numbers
    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
    for (int i = 0; i < networkInfos.length; i++) {
        if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
            if (networkInfos[i].getType() == ConnectivityManager.TYPE_MOBILE) {
                isWifiConnect = false;
            }
            if (networkInfos[i].getType() == ConnectivityManager.TYPE_WIFI) {
                isWifiConnect = true;
            }
        }
    }
    return isWifiConnect;
}
 
Example 6
Source File: NetWorkHelper.java    From browser with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 判断是否有网络连
 */
public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 7
Source File: ToolsUtil.java    From SimpleNews with Apache License 2.0 6 votes vote down vote up
/**
 * 判断网络是否可用
 * @param context
 * @return
 */
public static boolean isNetworkAvailable(Context context) {
	ConnectivityManager cm = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (cm != null) {
		//如果仅仅是用来判断网络连接
		//则可以使用 cm.getActiveNetworkInfo().isAvailable();
		NetworkInfo[] info = cm.getAllNetworkInfo();
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				if (info[i].getState() == NetworkInfo.State.CONNECTED) {
					return true;
				}
			}
		}
	}
	return false;
}
 
Example 8
Source File: RxNetTool.java    From RxTools-master with Apache License 2.0 6 votes vote down vote up
/**
 * 判断网络连接是否可用
 */
public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
    } else {
        //如果仅仅是用来判断网络连接
        //则可以使用 cm.getActiveNetworkInfo().isAvailable();
        NetworkInfo[] info = cm.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 9
Source File: NetworkConnUtil.java    From RairDemo with Apache License 2.0 6 votes vote down vote up
/**
 * 网络是否能够连接,能够寻找到网络
 *
 * @param context
 * @return
 */
public static boolean isNetWorkAvilable(Context context) {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        Log.e(TAG, "couldn't get connectivity manager");
    } else {
        NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
        if (networkInfos != null) {
            for (int i = 0, count = networkInfos.length; i < count; i++) {
                if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 10
Source File: CommonUtils.java    From Mobike with Apache License 2.0 6 votes vote down vote up
public static boolean isNetworkAvailable(Context context) {
    if (context == null) {
        return false;
    }

    ConnectivityManager connectivity =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            int l = info.length;
            for (int i = 0; i < l; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 11
Source File: DaemonService.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
public boolean isNetworkAvailable()
{
	Context context = getApplicationContext();
	ConnectivityManager connect = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (connect == null)
	{
		return false;
	}
	else
	{
		// get all network info
		NetworkInfo[] info = connect.getAllNetworkInfo();
		if (info != null)
		{
			for (int i = 0; i < info.length; i++)
			{
				if (info[i].getState() == NetworkInfo.State.CONNECTED)
				{
					return true;
				}
			}
		}
	}

	return false;
}
 
Example 12
Source File: ShowWebView.java    From webview with GNU General Public License v2.0 6 votes vote down vote up
private boolean haveNetworkConnection() {
      boolean haveConnectedWifi = false;
      boolean haveConnectedMobile = false;

      ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo[] netInfo = cm.getAllNetworkInfo();

  for (NetworkInfo ni : netInfo) {
     if (ni.getTypeName().equalsIgnoreCase("WIFI"))
        if (ni.isConnected())
            haveConnectedWifi = true;
    if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
        if (ni.isConnected())
            haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
 
Example 13
Source File: OkUtils.java    From BlueBoard with Apache License 2.0 6 votes vote down vote up
/**
 * 检查网络
 */
public static boolean checkNetState(Context context) {
    boolean netstate = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    netstate = true;
                    break;
                }
            }
        }
    }
    return netstate;
}
 
Example 14
Source File: NetworkUtil.java    From BaseProject with MIT License 6 votes vote down vote up
/**
 * 网络是否可用
 */
public static boolean isNetworkAvailable(@NonNull Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE);
    if (null == manager) {
        return false;
    }
    NetworkInfo[] info = manager.getAllNetworkInfo();
    if (info != null) {
        for (NetworkInfo anInfo : info) {
            if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}
 
Example 15
Source File: NetUtils.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
public static NETWORK_STATUS getNetworkType(Context context) {
	NETWORK_STATUS ret = NETWORK_STATUS.STATE_NONE_NETWORK;

	ConnectivityManager connetManager = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (connetManager == null) {
		Log.e(TAG, "isNetWorkAvailable connetManager = null", Log.APP);
		return ret;
	}
	NetworkInfo[] infos = connetManager.getAllNetworkInfo();
	if (infos == null) {
		return ret;
	}
	for (int i = 0; i < infos.length && infos[i] != null; i++) {
		if (infos[i].isConnected() && infos[i].isAvailable()) {

			if (infos[i].getTypeName().equalsIgnoreCase(NETTYPE_WIFI)) {
				ret = NETWORK_STATUS.STATE_WIFI;
			} else {
				ret = NETWORK_STATUS.STATE_GPRS;
			}

			break;
		}
	}

	Log.i(TAG, "get network stype is : " + ret, Log.APP);
	return ret;

}
 
Example 16
Source File: NetworkUtil.java    From MyWeather with Apache License 2.0 5 votes vote down vote up
public static boolean checkNetwork(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
    for (int i = 0; i < networkInfos.length; i++) {
        if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }
    }

    return false;
}
 
Example 17
Source File: NetUtils.java    From SimplifyReader with Apache License 2.0 5 votes vote down vote up
public static boolean isNetworkAvailable(Context context) {
	ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo[] info = mgr.getAllNetworkInfo();
	if (info != null) {
		for (int i = 0; i < info.length; i++) {
			if (info[i].getState() == NetworkInfo.State.CONNECTED) {
				return true;
			}
		}
	}
	return false;
}
 
Example 18
Source File: NetworkUtils.java    From YCVideoPlayer with Apache License 2.0 5 votes vote down vote up
private static boolean isConnected(Context context, int type) {
    //getAllNetworkInfo() 在 API 23 中被弃用
    //getAllNetworks() 在 API 21 中才添加
    ConnectivityManager manager = (ConnectivityManager) context.
            getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        NetworkInfo[] allNetworkInfo = new NetworkInfo[0];
        if (manager != null) {
            allNetworkInfo = manager.getAllNetworkInfo();
        }
        for (NetworkInfo info : allNetworkInfo) {
            if (info.getType() == type) {
                return info.isAvailable();
            }
        }
    } else {
        Network[] networks = new Network[0];
        if (manager != null) {
            networks = manager.getAllNetworks();
        }
        for (Network network : networks) {
            NetworkInfo networkInfo = manager.getNetworkInfo(network);
            if (networkInfo.getType() == type) {
                return networkInfo.isAvailable();
            }
        }
    }
    return false;
}
 
Example 19
Source File: NetWorkHelper.java    From LLApp with Apache License 2.0 5 votes vote down vote up
public static boolean isNetConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        NetworkInfo[] infos = cm.getAllNetworkInfo();
        if (infos != null) {
            for (NetworkInfo ni : infos) {
                if (ni.isConnected()) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 20
Source File: ConnectionDetector.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
public boolean isConnectingToInternet() {

		ConnectivityManager connectivity = (ConnectivityManager) _context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivity != null)

		{

			NetworkInfo[] info = connectivity.getAllNetworkInfo();

			if (info != null)

				for (int i = 0; i < info.length; i++)

					if (info[i].getState() == NetworkInfo.State.CONNECTED)

					{
						return true;

					}

		}
		return false;

	}