我希望更准确地获取Android电池信息。
我找到了一些可以提供电池电量和音量的代码,但它并没有帮助我。
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
我需要更多信息。我需要电池电量最多2位十进制精度。
P.S。我看到路线'/ sys / class / power_supply / battery /'。但我没有找到我想要的信息。
答案 0 :(得分:1)
试试这个为我工作
private void getBatteryPercentage() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int currentLevel = intent.getIntExtra(
BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (currentLevel >= 0 && scale > 0) {
level = (currentLevel * 100) / scale;
}
Toast.makeText(getApplicationContext(), "Battery Level Remaining: " + level
+ "%", Toast.LENGTH_LONG).show();
}
};
IntentFilter batteryLevelFilter = new IntentFilter(
Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}