How get battery temperature with decimal?
Asked Answered
D

3

8

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?

Dansby answered 22/8, 2013 at 14:40 Comment(0)
U
22

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.

Upcountry answered 22/8, 2013 at 14:52 Comment(5)
something like float temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)/10;?Dansby
exactly. android just uses an int to save some memory since the battery won't reach higher than 4.3 Billion degrees that often.Upcountry
seems good. With your solution works.. But for now it's stuck "35.0 °C" .. i try to wait and see if something changeDansby
Using your example: int returnedValue = 270; float castedValue = ((float) returnedValue) / 10; have i to write it after int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);?Dansby
I don't understand where you have put the intent for the temperature in here int returnedValue = 270; float castedValue = ((float) returnedValue) / 10; i missed somethingDansby
S
3
    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";
    }
Senseless answered 8/4, 2014 at 23:38 Comment(0)
T
1

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;

Tinishatinker answered 22/8, 2013 at 14:52 Comment(5)
Thanks for the replay.. Is there any way to convert the Integer? This application for example has decimals lh4.ggpht.com/…Dansby
is possible do something like: int i = 56472201; float e = ((float) i)/1000000.0 double e = i / 1000000.0; ?Dansby
Do you know if the temperature is always ending with .0°? If it is, you can just change your int to a float like this: temp/10.0fToilette
in which sense "if the temperature is always ending with .0°?"Dansby
Sorry I thought the app only added the 0 on purpose, but the numbers you're getting can be divided by 10.0f, like this: temp/10.0f to get 1 decimal valueToilette

© 2022 - 2024 — McMap. All rights reserved.