Skip to content Skip to sidebar Skip to footer

Can Wifi Be Turned Off Programmatically

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

Solution 1:

Permissions are required.

I just wrote this app that toggles Wifi.

Manifest

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.stackoverflow.q5766518"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="3" /><uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE" /><uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE" /><uses-permissionandroid:name="android.permission.WAKE_LOCK" /><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><activityandroid:name=".Main"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

layout

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

Main Activity

@OverridepublicvoidonCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    finalButtonmyButton= (Button) findViewById(R.id.my_button);
    myButton.setOnClickListener(newView.OnClickListener()
    {
      @OverridepublicvoidonClick(View v)
      {
        finalWifiManagerwifi= (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifi.setWifiEnabled(!wifi.isWifiEnabled());
      }
    });
  }

Solution 2:

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.

Solution 3:

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.

Post a Comment for "Can Wifi Be Turned Off Programmatically"