0

当电池电量低于 20% 但应用程序在单击时崩溃时,我尝试使用此方法在单击切换按钮时禁用 wifi:

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);
    }
}

有任何想法吗?

4

2 回答 2

1

您的代码中似乎缺少几个 = 符号。尝试使用:

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);
}
于 2013-06-27T08:21:40.460 回答
0

您只能将 View 作为参数传递。不允许有意图。

于 2013-06-27T08:48:25.070 回答