Checking Wi-Fi enabled or not on Android

后端 未结 4 981
北恋
北恋 2020-12-09 01:30

What would the code be for checking whether the Wi-Fi is enabled or not?

相关标签:
4条回答
  • 2020-12-09 01:44

    The above answers work fine. But don't forget to add the right permissions in the Manifest:

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

    Hope it helps ..

    0 讨论(0)
  • 2020-12-09 01:53

    The top answer is correct, but not up to date because this code may leak memory on certain devices.

    Therefore the better answer would be:

    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifiManager.isWifiEnabled()) {
      // wifi is enabled
    }
    

    Permissions in app=>mainfests=>AndroidManifest.xml:

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

    Reference: https://www.mysysadmintips.com/other/programming/759-the-wifi-service-must-be-looked-up-on-the-application-context

    0 讨论(0)
  • 2020-12-09 02:02
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    if (wifiManager.isWifiEnabled()) {
      // wifi is enabled
    }
    

    For details check here

    0 讨论(0)
  • 2020-12-09 02:05
    public static boolean wifiState() {
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        return wifiManager.isWifiEnabled();
    }
    
    0 讨论(0)
提交回复
热议问题