WiFi On-Off in Android

Introduction

 
This article explains how to turn on and off the WiFi in Android. Android Studio is used to create the sample.
 
WiFi
 
WiFi stands for wireless fidelity. WiFi is the name of the popular wireless networking technology that uses radio waves to provide wireless high-speed internet and networking connections. WiFi is supported by many applications and devices, including video games, console, mobile phones, operating systems and other types of consumer electronics.
 
In an XML file, you first use the switch by which you can perform the on and off activity. Now in the Java file, you will create the id of the switch and set the switch on its setOnCheckedChangedListener. Create a method name toggleWifi that consists of arguments to check the status and which is of boolean type. This status will be ed true if we check the switch to on and return false if we check the switch to off. After setting the status to true the wifimanager will be started that sets the WiFi-enabled to true if the status is true and false if the status is false.   
 
Step 1
 
Create a project like this:
 
wifiExample
 
Step 2
 
Create an XML file and write the following.
 
In the XML file, you first take the switch by which you can perform the on and off activity.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.    
  7.     <ImageView  
  8.         android:id="@+id/imageView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerInParent="true"  
  12.         android:src="@drawable/images2" />  
  13.    
  14.     <Switch  
  15.         android:id="@+id/wifi_switch"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_alignParentBottom="true"  
  19.         android:layout_marginBottom="10dp"  
  20.         android:background="@android:color/background_dark"  
  21.         android:checked="false"  
  22.         android:text="Wi-Fi Settings"  
  23.         android:textColor="@android:color/white"  
  24.         android:textOff="OFF"  
  25.         android:textOn="ON" />  
  26.    
  27. </RelativeLayout> 
Step 3
 
Create a Java file and write the following.
 
In the Java file, you will create the id of the switch and set the switch on its setOnCheckedChangedListener. Create a method named toggleWifi that consists of an argument to check the status and that is of the boolean type. This status will be ed true if we check the switch to on and return false if we check the switch to off. After setting the status to true the wifimanager will be started that sets the WiFi-enabled to true if the status is true and false if the status is false.
  1. package com.wificheck;  
  2.   
  3. import android.net.wifi.WifiManager;  
  4. import android.os.Bundle;  
  5. import android.widget.CompoundButton;  
  6. import android.widget.Switch;  
  7. import android.widget.Toast;  
  8. import android.app.Activity;  
  9. import android.content.Context;  
  10.   
  11. public class MainActivity extends Activity   
  12. {  
  13.   
  14.  @Override  
  15.  protected void onCreate(Bundle savedInstanceState)   
  16.  {  
  17.   super.onCreate(savedInstanceState);  
  18.   setContentView(R.layout.activity_main);  
  19.   
  20.   Switch toggle = (Switch) findViewById(R.id.wifi_switch);  
  21.   toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()   
  22.   {  
  23.    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)   
  24.    {  
  25.     if (isChecked)   
  26.     {  
  27.      WiFitoggle(true);  
  28.      Toast.makeText(getApplicationContext(), " Enabled!", Toast.LENGTH_LONG).show();  
  29.     }   
  30.     else   
  31.     {  
  32.      WiFitoggle(false);  
  33.      Toast.makeText(getApplicationContext(), " Disabled!", Toast.LENGTH_LONG).show();  
  34.     }  
  35.    }  
  36.   });  
  37.  }  
  38.   
  39.  public void WiFitoggle(boolean s)   
  40.  {  
  41.   WifiManager wifiManager = (WifiManager) this  
  42.    .getSystemService(Context.WIFI_SERVICE);  
  43.   if (s == true && !wifiManager.isWifiEnabled())   
  44.   {  
  45.    wifiManager.setWifiEnabled(true);  
  46.   }   
  47.   else if (s == false && wifiManager.isWifiEnabled())   
  48.   {  
  49.    wifiManager.setWifiEnabled(false);  
  50.   }  
  51.  }  
Step 4
 
Android Menifest.xml file
 
You will write the permissions in the Android Manifest.xml file as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.wificheck"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.wificheck.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  27.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
  28.     <uses-permission android:name="android.permission.WAKE_LOCK" />  
  29. </manifest> 
Step 5
 
WiFi state change:
 
wifi1
 
wfi2
 
wifi3


Similar Articles