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?

David_D
  • 1,404
  • 4
  • 31
  • 65

3 Answers3

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.

M.Bennett
  • 1,019
  • 1
  • 10
  • 18
  • something like `float temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)/10;`? – David_D Aug 22 '13 at 15:05
  • exactly. android just uses an int to save some memory since the battery won't reach higher than 4.3 Billion degrees that often. – M.Bennett Aug 22 '13 at 15:09
  • seems good. With your solution works.. But for now it's stuck "35.0 °C" .. i try to wait and see if something change – David_D Aug 22 '13 at 15:11
  • `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);`? – David_D Aug 22 '13 at 15:13
  • 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 something – David_D Aug 22 '13 at 15:18
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";
    }
XXX
  • 8,996
  • 7
  • 44
  • 53
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;

Community
  • 1
  • 1
Raúl Juárez
  • 2,129
  • 1
  • 19
  • 17
  • Thanks for the replay.. Is there any way to convert the Integer? This application for example has decimals https://lh4.ggpht.com/yoR8kC9GTLEyKbFFYKohbW1P8z1L00_wYJjKykAIqhhbVMAFxbwiA02s4m9aTSwxBQ=h900-rw – David_D Aug 22 '13 at 14:55
  • is possible do something like: `int i = 56472201; float e = ((float) i)/1000000.0 double e = i / 1000000.0;` ? – David_D Aug 22 '13 at 15:00
  • 1
    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.0f – Raúl Juárez Aug 22 '13 at 15:01
  • in which sense "if the temperature is always ending with .0°?" – David_D Aug 22 '13 at 15:04
  • 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 value – Raúl Juárez Aug 22 '13 at 15:07