Enable And Disable Wi-Fi In Android Application Using Android Studio

Introduction

In this article, I will show you how to Enable and Disable WI-FI in Android apps using Android Studio. The information that an application can access includes connected network's link speed, IP address, negotiation state, other networks information. Applications can also scan, add, save, terminate, and initiate Wi-Fi connections.

Requirements

Steps should be followed

These are the steps to create a changed background and audio player Android app. I have included the source code below.

Step 1

Open Android Studio and start a new Android Studio Project.

 Android

Step 2

You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click "Next" button.

Android

Step 3

Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role in running the application.

Android

Step 4

Now, add the activity and click "Next" button.

Android

 Step 5

Add activity name and click "Finish".

Android

Step 6

Go to activity_main.xml. This XML file contains the designing code for the Android app in the activity_main.xml.

Android

The XML code is given below

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.    tools:context=".MainActivity">  
  6.   
  7.     <ImageView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/imageView"  
  11.         android:src="@drawable/wifi"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_alignParentStart="true"  
  14.         android:layout_marginStart="14dp" />  
  15.   
  16.     <Button  
  17.         android:id="@+id/button1"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_marginLeft="76dp"  
  21.         android:text="Enable Wifi"  
  22.         android:layout_alignParentStart="true"  
  23.         android:layout_marginStart="28dp"  
  24.         android:layout_alignBottom="@+id/button2" />  
  25.   
  26.     <Button  
  27.         android:id="@+id/button2"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:text="Disable Wifi"  
  31.         android:layout_marginBottom="52dp"  
  32.         android:layout_marginEnd="14dp"  
  33.         android:layout_alignParentBottom="true"  
  34.         android:layout_alignParentEnd="true" />  
  35.   
  36. </RelativeLayout>  

Step 7

Go to  Main Activity.java. This Java program is the backend language for Android.

Android

The java code is given below

  1. package abu.wifi;  
  2.   
  3. import android.net.wifi.WifiManager;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     Button enableButton,disableButton;  
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.   
  18.         enableButton=(Button)findViewById(R.id.button1);  
  19.         disableButton=(Button)findViewById(R.id.button2);  
  20.   
  21.         enableButton.setOnClickListener(new OnClickListener(){  
  22.             public void onClick(View v){  
  23.                 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  24.                 wifi.setWifiEnabled(true);  
  25.             }  
  26.         });  
  27.   
  28.         disableButton.setOnClickListener(new OnClickListener(){  
  29.             public void onClick(View v){  
  30.                 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  31.                 wifi.setWifiEnabled(false);  
  32.             }  
  33.         });  
  34.     }  
  35. }  

 

Step 8

 We need to Enable and Disable WI-FI. So, add WI-FI state permission in an AndroidManifest.xml.

Android

The AndroidManifest.xml Code is given below

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="abu.wifi">  
  4.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  5.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>  
  6.   
  7.     <application  
  8.         android:allowBackup="true"  
  9.         android:icon="@mipmap/ic_launcher"  
  10.         android:label="@string/app_name"  
  11.         android:roundIcon="@mipmap/ic_launcher_round"  
  12.         android:supportsRtl="true"  
  13.         android:theme="@style/AppTheme">  
  14.         <activity android:name=".MainActivity">  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.   
  23. </manifest>  

 Step 9

Now, go to menu bar and click "Make project" or press ctrl+f9 to debug it for errors.

Android

Step 10

Then, click the Run button or press shift+f10 to run the project. And choose the virtual machine and click OK.

Android

Conclusion

We have successfully created the "Enabled and Disabled WI-FI" Android application using Android Studio.

Android

Up Next
    Ebook Download
    View all
    Learn
    View all