当电池电量低于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);
}
}
有什么想法吗?
答案 0 :(得分: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);
}
答案 1 :(得分:0)
您只能将View作为参数传递。意图是不允许的。