Tutorials Android How to Turn off, Turn on Wifi in Android

How to Turn off, Turn on Wifi in Android

The following code snippet will help you to pragmatically turn on and turn off wi-fi in android. This sample code is using android Switch (Available from android API level 14) to toggle between wi-fi state.

How to Turn off, Turn on wifi in android using code

We need following permissions in AndroidMainfest.xml file

  <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" />

Defining the layout with on/off switch

<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/wifi" />

    <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>

Turn on or off wifi in Android

We are done with the layout. Now let us move on to turn on wifi pragmatically.  To do this e need to get the Wifi system service by calling getSystemService() method. After getting Wifi manager, we can check the state of WiFi by calling isWiiEnabled() method, before turning on.

package com.example.wifitest;
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) {
					toggleWiFi(true);
					Toast.makeText(getApplicationContext(), "Wi-Fi Enabled!", Toast.LENGTH_LONG).show();
				} else {
					toggleWiFi(false);
					Toast.makeText(getApplicationContext(), "Wi-Fi Disabled!", Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	public void toggleWiFi(boolean status) {
		WifiManager wifiManager = (WifiManager) this
				.getSystemService(Context.WIFI_SERVICE);
		if (status == true && !wifiManager.isWifiEnabled()) {
			wifiManager.setWifiEnabled(true);
		} else if (status == false && wifiManager.isWifiEnabled()) {
			wifiManager.setWifiEnabled(false);
		}
	}

}

Download Complete Example

Here you can download complete eclipse project source code from GitHub.

Download Complete Source Code from GitHub

Total
0
Shares
guest
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
yes
yes
8 years ago

you can download full code source for
http://www.andbrain.com/product/hotspotutility1-0-2/

Nihat Yıldız
Nihat Yıldız
8 years ago

teşekkürler

thank you

Aldy Rialdy Atmadja
Aldy Rialdy Atmadja
7 years ago

thanks is very useful

Samir Alger
Samir Alger
7 years ago
javatechig
javatechig
7 years ago
Reply to  Samir Alger

Thanks for sharing samir

ashish dhiman
ashish dhiman
6 years ago

Thanks

Santanu Barik
Santanu Barik
4 years ago

how do i enable or disable wifi by saying “WiFi on or WiFi off” through my assistant app programmatically????

Santanu Barik
Santanu Barik
4 years ago

How can i enable or disable wifi by saying “turn on wifi” or “turn off wifi ” from my assistant app???

Previous Post
Android Tutorials

How to Get List of Installed Apps in Android

Next Post
Android Tutorials

Android Seekbar Example

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
8
0
Would love your thoughts, please comment.x
()
x