Reputation: 739
What would the code be for checking whether the Wi-Fi is enabled or not?
Upvotes: 57
Views: 43566
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
Reputation: 9072
public static boolean wifiState() {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
return wifiManager.isWifiEnabled();
}
Upvotes: 8
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" />
Upvotes: 17