我正在尝试在某个代码后更换电池。我正在使用以下代码,但不幸的是没有运气。我得到零任何想法请
在上创建一个:
batteryIntent = registerReceiver(null,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
private double batteryLevel() {
int rawlevel = batteryIntent.getIntExtra("level", -1);
double scale = batteryIntent.getIntExtra("scale", -1);
double level = -1;
if (rawlevel >= 0 && scale > 0) {
level = rawlevel / scale;
}
return level;
}
在 onClick 上:
double startbatterylevel = batteryLevel();
//certain code
double estimatedbattery = startbatterylevel - endbatterylevel ;
Estbatterylevel.setText("estimated battery"+estimatedbattery);