how to check wifi or 3g network is available on android device

前端 未结 4 1245
说谎
说谎 2020-11-27 02:59

Here, my android device supports both wifi and 3g. At particular time which network is available on this device. Because my requirement is when 3g is available I have to upl

4条回答
  •  情书的邮戳
    2020-11-27 03:47

    You need to add below permission in android manifest file:

    
    

    After that you can use below functions to check wifi or mobile network is connected or not

    public static boolean isWifiConnected(Context context) {
            ConnectivityManager connManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            return ((netInfo != null) && netInfo.isConnected());
        }
    
    public static boolean isMobileConnected(Context context) {
            ConnectivityManager connManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            return ((netInfo != null) && netInfo.isConnected());
        }
    

    Some references from developer.android.com are:

    1. https://developer.android.com/reference/android/net/ConnectivityManager.html
    2. https://developer.android.com/reference/android/net/NetworkInfo.html
    3. https://developer.android.com/reference/android/net/ConnectivityManager.html#getActiveNetworkInfo()

提交回复
热议问题