inforg
inforg

Reputation: 739

Checking Wi-Fi enabled or not on Android

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

Upvotes: 57

Views: 43566

Answers (4)

McLan
McLan

Reputation: 2688

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 ..

Upvotes: 24

XXX
XXX

Reputation: 9072

public static boolean wifiState() {
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    return wifiManager.isWifiEnabled();
}

Upvotes: 8

KoKlA
KoKlA

Reputation: 988

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

Upvotes: 17

Rasel
Rasel

Reputation: 15477

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

For details check here

Upvotes: 118

Related Questions