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