How to turn on / turn off WiFi connection programmatically in Android

Published by Igor Khrupin on

Some Android applications can turn on/turn off WiFi on device. How to do same.

First of all you need specify uses-permission in Android Manifest file

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

And now you can enable wifi state via WiFiManager:

wifiManager.setWifiEnabled(true);

or disable:

wifiManager.setWifiEnabled(false);

Below you can download source code of sample android project

The source code of main Activity is:

package com.hrupin.wifiSample;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;
import com.hrupin.wifiSample.R;

public class WifiSwitcherExampleActivity extends Activity implements OnClickListener {
    private static ConnectivityManager connectivityManager;
    private ToggleButton tbWiFi;
    private WifiManager wifi;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        tbWiFi = (ToggleButton) findViewById(R.id.toggleButtonWiFi);
        tbWiFi.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initButton();
    }

    private void initButton() {
        if (isConnected(this, ConnectivityManager.TYPE_WIFI)) {
            tbWiFi.setChecked(true);
        }
    }

    private static boolean isConnected(Context context, int networkType) {
        NetworkInfo networkInfo = null;
        if (connectivityManager != null) {
            networkInfo = connectivityManager.getNetworkInfo(networkType);
        }
        return networkInfo == null ? false : networkInfo.isConnected();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == tbWiFi.getId()) {
            wifi.setWifiEnabled(tbWiFi.isChecked());
        }
    }
}

Android Manifest:

<!--?xml version="1.0" encoding="utf-8"?-->
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.hrupin.wifiSample" 
    android:versioncode="1" 
    android:versionname="1.0">

    <uses-sdk android:minsdkversion="8">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission></uses-permission></uses-sdk>

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:name=".WifiSwitcherExampleActivity" 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>
</manifest>

Here you can download the source code of sample Android application.

Download it from github

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.