--ADVERTISEMENT--

Previously I have posted a tutorial about how to check internet connection in android programmatically and in this tutorial, I am going to show how to check whether WiFi is connected or not in any android devices programmatically and do something if WIFI is connected otherwise do another things. While developing android application where we have used data from server or doing any activity related internet it is better to give first priory to WiFi rather than mobile network.

While updating something to our application we have to check first WiFi connection. If wifi is connected to the device then we start updating. While downloading and uploading if we use mobile network it consumes more data.

Here when you click the Check WIFI Connection button, you will see the wifi status just below the button in TextView.

Related:
Check Internet Connection in Android
Turn ON and OFF WiFi Connection Programmatically in Android
How to Get the IP Address of Android Device Programmatically

Android Example: Checking WiFi Connection in Android Programmatically


This is simple task to check wifi connection in android programmatically; just you have to add some lines of code in your java activity file.

Following are the XML and java file to check whether wifi is connected or not in android.

XML Layout File

In XML layout file I just have added a button and a textview in linear layout. Following is the complete content of XML layout file.

res/layout/checking_wifi_connection_android.xml

Java Activity File

We have to use ConnectivityManager to get the network information and to check wifi status. Following is the complete code of java activity file.

src/CheckingWiFiConnectionInAndroid.java

Now, we have to add permission in our app manifest file to access internet in our application. Your AndroidMainfest.xml file looks like this.

AndroidManifest.xml


Default content of strings.xml file.

res/values/strings.xml

Android Example: Checking WiFi Connection in Android Programmatically

Run your How to Check WiFi Connected or Not in Android Programmatically application and click Check WiFi Connection button, you will see the status of wifi below the button.

Post a Comment

Powered by Blogger.