try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start_wifi);
stop = (Button) findViewById(R.id.stop_wifi);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
WifiManager wifi = (WifiManager) MainActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
wifi.setWifiEnabled(true);
Toast.makeText(MainActivity.this, "Turn ON WIFI",
Toast.LENGTH_LONG).show();
}
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
WifiManager wifi = (WifiManager) MainActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()) {
wifi.setWifiEnabled(false);
Toast.makeText(MainActivity.this, "Turn OFF WIFI",
Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) {
Log.v("MainActivity Exception", Log.getStackTraceString(e));
}
}
In Manifest
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>