Turn WiFi On and Off Using Android App

In this article you will learn how to turn WiFi On and Off using an Android app. See the following procedure.

Step 1

First create an Andorid Application Project.

Andorid Application Project

Step 2

Provide a name for the project and select SDK Requirements.

select SDK Requirement

Step 3

Now configure the Android project as you choose.

Configure Android Project

Step 4

Create an icon for the Android project.

Create Icon

Step 5

Select Activity for Android Project.

blank activity

Step 6

Select an Activity name and Layout name.

finish

Step 7

Then open the activity_main.xml file.

Drag a Switch Element and one TextView Element to the Graphical Layout of activity_main.xml.

Activity_main.xml file code

  1.       
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context="com.example.wifiexample.MainActivity" >  
  12.     <Switch  
  13.     android:id="@+id/switch1"  
  14.     android:layout_width="wrap_content"  
  15.     android:layout_height="wrap_content"  
  16.     android:layout_marginLeft="27dp"  
  17.     android:layout_marginTop="72dp"  
  18.     android:text="Switch" />  
  19.     <TextView  
  20.     android:id="@+id/textView1"  
  21.     android:layout_width="wrap_content"  
  22.     android:layout_height="wrap_content"  
  23.     android:layout_alignRight="@+id/switch1"  
  24.     android:layout_below="@+id/switch1"  
  25.     android:layout_marginRight="49dp"  
  26.     android:layout_marginTop="88dp"  
  27.     android:text="TextView" />  
  28. </RelativeLayout>  
Step 8

Now go to the AndroidManifest.xml file and add two user permissions as in the following:
  1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  
  2. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>  
Step 9

Then open the MainActivity.java file and write some code:
  1. package com.example.wifiexample;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.net.wifi.WifiInfo;  
  6. import android.net.wifi.WifiManager;  
  7. import android.os.Bundle;  
  8. import android.view.Menu;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.MenuItem;  
  11. import android.view.View;  
  12. import android.widget.Switch;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity {  
  17.     Switch s;  
  18.     WifiManager wm;  
  19.     TextView t;  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_main);  
  25.         wm=(WifiManager)getSystemService(Context.WIFI_SERVICE);  
  26.         s=(Switch)findViewById(R.id.switch1);  
  27.         t=(TextView)findViewById(R.id.textView1);  
  28.         s.setOnClickListener(new OnClickListener() {  
  29.               
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 // TODO Auto-generated method stub  
  33.                 if(s.isChecked())  
  34.                 {  
  35.                     wm.setWifiEnabled(true);  
  36.                     getwifi();  
  37.                     Toast.makeText(MainActivity.this"WIFI is ON"3000).show();  
  38.                 }  
  39.                 else{  
  40.                     wm.setWifiEnabled(false);  
  41.                     getwifi();  
  42.                     Toast.makeText(MainActivity.this"WIFI is OFF"3000).show();  
  43.                 }  
  44.             }  
  45.         });  
  46.         getwifi();  
  47.           
  48.     }  
  49.     public void getwifi()  
  50.     {  
  51.         if(wm.isWifiEnabled()){  
  52.             s.setChecked(true);  
  53.             Toast.makeText(this"WIFI is ON"3000).show();  
  54.             WifiInfo w = wm.getConnectionInfo();  
  55.             t.setText("BSSID : " + w.getBSSID() + "\n");  
  56.             t.append("RSSID : " + w.getRssi() + "\n");  
  57.             t.append("SIGNAL : " + w.getLinkSpeed() + "\n");  
  58.             t.append("SSID : " + w.getSSID() + "\n");  
  59.         }  
  60.         else  
  61.         {  
  62.             t.setText("Not Connected");  
  63.         }  
  64.     }  
  65.       
  66.     @Override  
  67.     public boolean onCreateOptionsMenu(Menu menu) {  
  68.         // Inflate the menu; this adds items to the action bar if it is present.  
  69.         getMenuInflater().inflate(R.menu.main, menu);  
  70.         return true;  
  71.     }  
  72.   
  73.     @Override  
  74.     public boolean onOptionsItemSelected(MenuItem item) {  
  75.         // Handle action bar item clicks here. The action bar will  
  76.         // automatically handle clicks on the Home/Up button, so long  
  77.         // as you specify a parent activity in AndroidManifest.xml.  
  78.         int id = item.getItemId();  
  79.         if (id == R.id.action_settings) {  
  80.             return true;  
  81.         }  
  82.         return super.onOptionsItemSelected(item);  
  83.     }  
  84. }  
Step 10

Now run the app in a real Andorid phone because the AVD does not support this app.

Run the App

Run the App In Andorid Phone

Enjoy.

 

Up Next
    Ebook Download
    View all
    Learn
    View all
    
    View All Comments