First you need to permissions - to read Wifi state and to change it and here is manifest
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.dynamix.mobile.wifi"
- android:versionCode="1"
- android:versionName="1.0.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name="WifiToggle"
- 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>
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
- </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
when will use wifi manager to handle our operations to get the instance we'll do
Using java Syntax Highlighting
- mWm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
And then will try to change settings by,
mWm.setWifiEnabled(false/true) to toggle wifi.
You can go all fancy to check the result by using getWifiState() to get better state of your wifi instead of isWifiEnabled()
full java code :
Using java Syntax Highlighting
- public class WifiToggle extends Activity implements
- CompoundButton.OnCheckedChangeListener {
- private CheckBox cb;
- private WifiManager mWm;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mWm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
- LinearLayout mainView = (LinearLayout) findViewById(R.id.top);
- cb = (CheckBox) findViewById(R.id.check);
- cb.setOnCheckedChangeListener(this);
- if (mWm.isWifiEnabled()) {
- cb.setChecked(true);
- cb.setText("Wifi is : enabled");
- } else {
- cb.setChecked(false);
- cb.setText("Wifi is : disabled");
- }
- }
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (isChecked) {
- if (mWm.setWifiEnabled(true))
- cb.setText("Wifi is : enabled");
- else {
- cb.setChecked(false);
- cb.setText("Attempt to enable failed");
- }
- } else {
- if ( (mWm.setWifiEnabled(false)))
- cb.setText("Wifi is : disabled");
- else {
- cb.setChecked(true);
- cb.setText("Attempt to disable failed");
- }
- }
- }
- }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
Layout is just a Linear layout with button inside.
Corrections or additions to the tut are welcome.


