Question

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;
}
Was it helpful?

Solution

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

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

OTHER TIPS

I'm guessing that you checked that R.string.menu_wifi really is connected to the right menu item? As far as I can tell, if you don't get the toasts, you dont even get into the wifiStatus method.

edit: Shouldn't you be gunning for R.id.wifi_item?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top