In my application i have a toggle button that needs to activate or disable the wifi.
public void getRisparmio(View view) {
// is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
WifiManager wifiManager;
if (on) {
wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
} else {
wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
}
Now the problem is: if the wifi is already activated and I launch the app, the toggle button should be pressed/activated. Actually doesn't go and the toggle always resume its state on off. I think i can check the state onResume()
but i don't know how and i don't know if i have to add something in the onCreate()
method too.
How can i do to check the state? Thanks
You will have to set the toggle button in onCreate() and onResume() using the toggle button's method as such:
toggleButton.setChecked(wifiManager.isWifiEnabled());
E.g.
@Override
protected void onResume()
{
super.onResume();
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
toggleButton.setChecked(wifiManager.isWifiEnabled());
}