0

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

Sam
  • 7,252
  • 16
  • 46
  • 65
David_D
  • 1,404
  • 4
  • 31
  • 65
  • Are you listening for change in the wifi state and toggling change of button state? – frogmanx Jun 26 '13 at 07:59
  • The code is which i posted. Actually when i click the toggle goes in on state and the wifi turns on, click again and the wifi turns off. The application goes well but there is the problem i wrote. The state of the toggle button needs to be according to the state the wifi is in when i open the application. – David_D Jun 26 '13 at 08:05

1 Answers1

0

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()); 
}
frogmanx
  • 2,620
  • 1
  • 18
  • 20
  • something like: `@Override` `protected void onResume() {` `super.onResume();` `toggleButton.setChecked(wifiManager.isWifiEnabled());` `}` ? – David_D Jun 26 '13 at 08:09
  • you will have to redeclare the variables, but in essence yes. – frogmanx Jun 26 '13 at 08:14
  • mmh, "Cannot make a static reference to the non-static method isWifiEnabled() from the type WifiManager." But `isWifiEnabled()` from where you take it? – David_D Jun 26 '13 at 08:15
  • what's ur id for togglebutton? – frogmanx Jun 26 '13 at 08:21
  • it's toggleButton1.. I think your method is correct but i don't know i have this error: "Cannot make a static reference to the non-static method setChecked(boolean) from the type ToggleButton." – David_D Jun 26 '13 at 08:29
  • Yeah, we may miscommunicate with each other since the variable names are same as their respective class names, I'll redo with this new info. – frogmanx Jun 26 '13 at 08:31
  • have i to create a local variable? And set it `null`? – David_D Jun 26 '13 at 08:32
  • Ok, no errors `onResume()` butin the `onCreate()` i get this one : "wifiManager cannot be resolved" and ask me to create a variable. Have i to write everything that is inside in the `onResume()` in the `onCreate()`? or only `toggleButton.setChecked(wifiManager.isWifiEnabled());` ? – David_D Jun 26 '13 at 08:39
  • Yes, rewrite the whole bit in oncreate – frogmanx Jun 26 '13 at 08:45