2

How can wifi be turned off/on programmatically and do is rooted or system app required for this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Androider
  • 21,125
  • 36
  • 99
  • 158

3 Answers3

6

Permissions are required.

I just wrote this app that toggles Wifi.

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stackoverflow.q5766518"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="3" />

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

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

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/my_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toggle Wifi" />
</LinearLayout>

Main Activity

 @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button myButton = (Button) findViewById(R.id.my_button);
    myButton.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifi.setWifiEnabled(!wifi.isWifiEnabled());
      }
    });
  }
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
Thane Anthem
  • 4,093
  • 4
  • 26
  • 24
1

WIFI_ON is a secure setting; only apps signed by the system firmware will be able to hold the proper permission and adjust it using the SDK.


UPDATE

setWifiEnabled() probably supports this, as was pointed out in the comments. I don't see evidence of a permission being required, but if there is one, you'll get a stack trace that should point out what's needed. My apologies for forgetting about this path.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    What about WifiManager and all the app widgets that toggle Wifi on/off? http://developer.android.com/reference/android/net/wifi/WifiManager.html – Thane Anthem Apr 23 '11 at 20:30
  • 1
    @Thane Anthem: Ah, my apologies. `setWifiEnabled()` would seem to support this, and a quick scan of the source code does not show any sign of a permission being needed (which is moderately disturbing). I will update my answer momentarily. – CommonsWare Apr 23 '11 at 20:37
  • Permissions do appear to be required, as I received errors about lacking three permissions while writing the app I posted elsewhere in this thread. – Thane Anthem Apr 23 '11 at 21:23
  • @Thane Anthem: Well, at least they are all ones you can ask for (vs. having to be a firmware-signed app). – CommonsWare Apr 23 '11 at 21:24
0

Yes, its possible. wifimangr.setWifiEnabled(false);

Create an object of Wifimanager..and call the method setWifiEnabled to "false". wifimangr.setWifiEnabled(false);

you need CHANGE_WIFI_STATE permission todo this.

Jashan PJ
  • 4,177
  • 4
  • 27
  • 41