문제

How can i get the battery temperature with decimal? Actually i can calculate it with

int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);

But in this way the result will be for example 36 °C.. I want something that show me 36.4 °C How can i do?

도움이 되었습니까?

해결책

Google says here :

Extra for ACTION_BATTERY_CHANGED: integer containing the current battery temperature.

The returned value is an int representing, for example, 27.5 Degrees Celcius as "275" , so it is accurate to a tenth of a centigrade. Simply cast this to a float and divide by 10.

Using your example:

int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
float tempTwo = ((float) temp) / 10;

OR

float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0) / 10;

You don't need to worry about the 10 as an int since only one operand needs to be a float for the result to be one too.

다른 팁

    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";
    }

That's the only way I know you can get the battery temperature, and is always an int.

According to documentation:

public static final String EXTRA_TEMPERATURE

Extra for ACTION_BATTERY_CHANGED: integer containing the current battery temperature.

But you can divide by 10.0f to get one decimal.

float ftemp = temp/10.0f;

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top