我正在寫一個應用程序,當電池電量少於10%的WiFi連接(如果激活)將禁用。這是我想要的,但不是我能夠做到的:)。我創建了一個切換,按下了停止wifi的,反之亦然時如果再按一次的WiFi這樣打開:當電池電量少於10%時,我如何禁用wifi?
public void getRisparmio(View view) {
// is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
WifiManager wifiManager;
if (on) {
wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
} else {
wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(tru);
}
}
這樣我得到的電池級:
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
什麼我想是類似於
public void getRisparmio(View view) {
// is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
WifiManager wifiManager;
if (on) {
if (level < 10){
wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
} else {
wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}
}
這可能嗎?由於
檢查這裏的答案http://stackoverflow.com/questions/14560341/disable-wifi-when-battery-voltage-is-3-4 –