How do I see if Wi-Fi is connected on Android?

前端 未结 22 2611
挽巷
挽巷 2020-11-22 05:56

I don\'t want my user to even try downloading something unless they have Wi-Fi connected. However, I can only seem to be able to tell if Wi-Fi is enabled, but they could sti

相关标签:
22条回答
  • 2020-11-22 06:09

    You should be able to use the ConnectivityManager to get the state of the Wi-Fi adapter. From there you can check if it is connected or even available.

    ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    
    if (mWifi.isConnected()) {
        // Do whatever
    }
    

    NOTE: It should be noted (for us n00bies here) that you need to add

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    to your

    AndroidManifest.xml for this to work.

    NOTE2: public NetworkInfo getNetworkInfo (int networkType) is now deprecated:

    This method was deprecated in API level 23. This method does not support multiple connected networks of the same type. Use getAllNetworks() and getNetworkInfo(android.net.Network) instead.

    NOTE3: public static final int TYPE_WIFI is now deprecated:

    This constant was deprecated in API level 28. Applications should instead use NetworkCapabilities.hasTransport(int) or requestNetwork(NetworkRequest, NetworkCallback) to request an appropriate network. for supported transports.

    0 讨论(0)
  • 2020-11-22 06:11

    Since the method NetworkInfo.isConnected() is now deprecated in API-23, here is a method which detects if the Wi-Fi adapter is on and also connected to an access point using WifiManager instead:

    private boolean checkWifiOnAndConnected() {
        WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    
        if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON
    
            WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    
            if( wifiInfo.getNetworkId() == -1 ){
                return false; // Not connected to an access point
            }
            return true; // Connected to an access point
        }
        else {
            return false; // Wi-Fi adapter is OFF
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:11

    Here is what I use as a utility method in my apps:

    public static boolean isDeviceOnWifi(final Context context) {
            ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            return mWifi != null && mWifi.isConnectedOrConnecting();
    }
    
    0 讨论(0)
  • 2020-11-22 06:11

    In new version Android

    private void getWifiInfo(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network[] networks = connManager.getAllNetworks();
    
        if(networks == null || networks.length == 0)
            return;
    
        for( int i = 0; i < networks.length; i++) {
            Network ntk = networks[i];
            NetworkInfo ntkInfo = connManager.getNetworkInfo(ntk);
            if (ntkInfo.getType() == ConnectivityManager.TYPE_WIFI && ntkInfo.isConnected() ) {
                final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
                if (connectionInfo != null) {
                    // add some code here
                }
            }
    
        }
    }
    

    and add premission too

    0 讨论(0)
  • 2020-11-22 06:11

    Add this for JAVA:

    public boolean CheckWifiConnection() {
            ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
            if (conMgr.getActiveNetworkInfo() != null
                    && conMgr.getActiveNetworkInfo().isAvailable()
                    && conMgr.getActiveNetworkInfo().isConnected()) {
                return true;
            } else {
                return false;
            }
        }
    

    in Manifest file add the following permissions:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
    0 讨论(0)
  • 2020-11-22 06:13

    Using WifiManager you can do:

    WifiManager wifi = (WifiManager) getSystemService (Context.WIFI_SERVICE);
    if (wifi.getConnectionInfo().getNetworkId() != -1) {/* connected */}
    

    The method getNeworkId returns -1 only when it's not connected to a network;

    0 讨论(0)
提交回复
热议问题