Question

I am stuck in this android app I am creating that has a toggle button. OnCreate I want it to get the wifi state, I have seen some people use ! In their code ex.(!isWifiEnabled) and I want to know what that ! means to see if I can implement that in my code instead of using the code below. This is all going in the onClick action of the ToggleButton.

if(wifiManager.isWifiEnabled ==true){
    wifiManager.setWifiEnabled(false);
}
Was it helpful?

Solution

The "!" means negation if a function returns true you are inverting the out put here that is

!true isnothing but false and !false is true

Your code is

if(wifiManager.isWifiEnabled ==true){
wifiManager.setWifiEnabled(false);

}

you can replace the if condition with

if(wifiManager.isWifiEnabled)

as long as it returns a boolean true value and if you want to check if wifi is not turned on then you can use

if(!wifiManager.isWifiEnabled)

hope this answer helped you

OTHER TIPS

When you put a "!" in front of a method that returns a Boolean value, you invert the value you are checking for. I know the wording may be a little confusing, so hopefully this example will clear things up.

if (!true) { //this should read "if not true"
    System.out.println("We got false.");
} else if (true) {
    System.out.println("We got true.");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top