Search code examples
javaandroidwifiandroid-menu

wifi doesn't turn on/off on menu option


I'm trying to change the wifi status with a menu option, but nothing happnes either the Toast I've created to advert that the wifi was turned on or of.also, I looked in this other post, but nothing

any hints or ideas will be good received. Here is my code:

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.string.Menu_mainMenu:
            mainMenu();

        case R.string.menu_wifi:
            wifiStatus();

        }

        return super.onOptionsItemSelected(item);
    }
    public void mainMenu(){

        Intent inte = new Intent(getApplicationContext(), DataconectActivity.class);
        startActivity(inte);
    }
    public void wifiStatus(){
        final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled()){
    wifi.setWifiEnabled(false);
    Toast.makeText(getApplicationContext(), R.string.wifioff, Toast.LENGTH_LONG);
}
else{
    wifi.setWifiEnabled(true);
    Toast.makeText(getApplicationContext(), R.string.wifion, Toast.LENGTH_LONG);
}
return;
}

Solution

  • So ... you're missing the .show() for the Toast:

    Toast.makeText(getApplicationContext(), R.string.wifion, Toast.LENGTH_LONG).show();