【发布时间】:2013-08-22 14:40:16
【问题描述】:
如何获取带小数的电池温度?其实我可以用
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
但是通过这种方式,结果将是例如 36 °C.. 我想要一些可以告诉我的东西 36.4 °C 我该怎么做?
【问题讨论】:
标签: android android-intent battery temperature
如何获取带小数的电池温度?其实我可以用
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
但是通过这种方式,结果将是例如 36 °C.. 我想要一些可以告诉我的东西 36.4 °C 我该怎么做?
【问题讨论】:
标签: android android-intent battery temperature
谷歌说here:
ACTION_BATTERY_CHANGED 的附加值:整数,包含当前电池温度。
返回值是一个int,例如27.5摄氏度为“275”,所以精确到十分之一摄氏度。只需将其转换为浮点数并除以 10。
使用您的示例:
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
float tempTwo = ((float) temp) / 10;
或
float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0) / 10;
您不必担心 10 作为 int,因为只有一个操作数需要是浮点数,结果也是一个。
【讨论】:
float temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)/10;?
Using your example: int returnedValue = 270; float castedValue = ((float) returnedValue) / 10; 要我写在int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);之后吗?
int returnedValue = 270; float castedValue = ((float) returnedValue) / 10;我错过了一些东西
public static String batteryTemperature(Context context)
{
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)) / 10;
return String.valueOf(temp) + "*C";
}
【讨论】:
这是我知道您可以获取电池温度的唯一方法,并且始终是 int。
根据文档:
公共静态最终字符串EXTRA_TEMPERATURE
ACTION_BATTERY_CHANGED 的附加值:整数 包含当前电池 温度。
但你可以除以 10.0f 得到一位小数。
float ftemp = temp/10.0f;
【讨论】:
int i = 56472201; float e = ((float) i)/1000000.0 double e = i / 1000000.0; ?