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 intrnet and networking connections. WiFi is supported by many applications and devices, including video games, console, moble phones, operating systems and other types of consumer electronics.
In a 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:
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.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/images2" />
<Switch
android:id="@+id/wifi_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:background="@android:color/background_dark"
android:checked="false"
android:text="Wi-Fi Settings"
android:textColor="@android:color/white"
android:textOff="OFF"
android:textOn="ON" />
</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 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.
package com.wificheck;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Switch toggle = (Switch) findViewById(R.id.wifi_switch);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
WiFitoggle(true);
Toast.makeText(getApplicationContext(), " Enabled!", Toast.LENGTH_LONG).show();
} else {
WiFitoggle(false);
Toast.makeText(getApplicationContext(), " Disabled!", Toast.LENGTH_LONG).show();
}
}
});
}
public void WiFitoggle(boolean s) {
WifiManager wifiManager = (WifiManager) this
.getSystemService(Context.WIFI_SERVICE);
if (s == true && !wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
} else if (s == false && wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
}
}
Step 4
Android Menifest.xml file
You will write the permissions in the Android Manifest.xml file as in the following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wificheck"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.wificheck.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>
Step 5
WiFi state change: