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

前端 未结 4 1232
说谎
说谎 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条回答
  •  萌比男神i
    2020-11-27 03:49

    First get a reference to the ConnectivityManager and then check the Wifi and 3G status of the device. You'll need the ACCESS_NETWORK_STATE permission to use this service.

        ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    
        if (mWifi.isAvailable() == true) {
            return "Connected to WiFi";
        } else if (mMobile.isAvailable() == true) {
            return "Connected to Mobile Network";
        } else return "No internet Connection"
    

提交回复
热议问题