I've tryied this method to disable wifi on click of a toggle button when the battery is less 20% but the application crash on click:

public void getRisparmio(View view, Intent intent) {
    // is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

    WifiManager wifiManager;
    if (on && level<20) {
      wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
      wifiManager.setWifiEnabled(false);
    } else {
      wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
      wifiManager.setWifiEnabled(true);
    }
}

Any ideas?

有帮助吗?

解决方案

It seems like you're missing a couple of = signs in your code. Try using:

if (on && level<20) {
  wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  wifiManager.setWifiEnabled(false);
} else {
  wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  wifiManager.setWifiEnabled(true);
}

其他提示

You can pass only View as an argument. Intent is not allowed.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top