0

in my application i display the battery level.. i get the battery level in this way:

int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

And it's ok i can see the percentage.. but online i found another way:

int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPct = level / (float)scale;

What's the difference? Why i would divide the level with the scale? Thanks

4

2 回答 2

1

好吧,你得到不同的数字:)

比例尺是 所以用第二种方法你得到你剩下integer containing the maximum battery level.的百分比(因此被称为)。batteryPct首先,您将获得电池电量的绝对数字。它被定义为integer field containing the current battery level, from 0 to EXTRA_SCALE.

于 2013-07-10T15:22:35.473 回答
1

根据Android docs, SCALE 是最大值:

公共静态最终字符串EXTRA_SCALE

在 API 级别 5 中添加了额外的 ACTION_BATTERY_CHANGED:包含最大电池电量的整数。

常数值:“比例”

LEVEL是当前级别:

公共静态最终字符串EXTRA_LEVEL

在 API 级别 5 中添加了额外的 ACTION_BATTERY_CHANGED:包含当前电池级别的整数字段,从 0 到 EXTRA_SCALE。

常数值:“水平”

所以基本上,第一个给你一个绝对数字,第二个给你一个百分比。

于 2013-07-10T15:22:05.790 回答