18

How do I get the temperature of the battery in android?

Christian
  • 25,249
  • 40
  • 134
  • 225

5 Answers5

13

http://developer.android.com/reference/android/os/BatteryManager.html

public static final String EXTRA_TEMPERATURE
Extra for ACTION_BATTERY_CHANGED: integer containing the current battery temperature.

Select0r
  • 12,234
  • 11
  • 45
  • 68
  • 1
    What is ACTION_BATTERY_CHANGED about? Do I have to lunch an intent to get the temperature? Do I have to listen to some system wide intent? – Christian Oct 22 '10 at 13:59
  • Google is your friend: http://www.tutorialforandroid.com/2009/01/getting-battery-information-on-android.html That tutorial is about how to get the battery level but together with the docs-link I posted, I'm sure you can figure out how to get the battery-temperature, just use "temperature" instead of "level" ... – Select0r Oct 22 '10 at 14:26
  • 10
    @Christian: You do not need to register an actual `BroadcastReceiver` if you do not want to. Call `registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED))`, and it will return the last Intent that was broadcast for this action. Use the `EXTRA_TEMPERATURE` extra to get the value you seek. – CommonsWare Oct 22 '10 at 14:38
  • Great post, I done some sample app to read the temperature and it works great but I can't not figured out what is the unit of the result here is my post (http://stackoverflow.com/questions/7716054/getting-the-battery-temperature-and-unit-of-the-result) – Lukap Oct 10 '11 at 20:16
  • they should really make the "null" version a separate method with a different name rather than overload it's meaning. Thanks for the comment though I didn't even think to look it up there. – Archimedes Trajano Feb 09 '12 at 19:29
  • @Select0r link is broken – Eric Schnipke Jan 20 '23 at 15:00
10

Try this:

private class mBatInfoReceiver extends BroadcastReceiver{ 

    int temp = 0;

    float get_temp(){
        return (float)(temp / 10);
    }

    @Override 
    public void onReceive(Context arg0, Intent intent) {
        temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
    }

};

then define in your Variable declarations:

private mBatInfoReceiver myBatInfoReceiver;

and in onCreate:

    @Override 
    public void onCreate(Bundle b) { 
        super.onCreate(b);  
        setContentView(R.layout.activity_main);

        // ...
        // Add this

        myBatInfoReceiver = new mBatInfoReceiver();                                     

        this.registerReceiver(this.myBatInfoReceiver,
                              new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

     } 

later call e.g in a OnClickListener()

float temp = myBatInfoReceiver.get_temp(); 

String message = "Current " + BatteryManager.EXTRA_TEMPERATURE + " = " +
                  temp +  Character.toString ((char) 176) + " C";
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
Ingo
  • 5,239
  • 1
  • 30
  • 24
9
    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
  • Does this do the same job as the accepted answer? If so, why is it so much shorter? Has it all been inlined? Would be great to have a quick description. Thanks! – Cornelius Roemer Dec 04 '19 at 17:02
  • @CorneliusRoemer because it is a sticky intent/sticky broadcast, for more detail https://developer.android.com/training/monitoring-device-state/battery-monitoring https://stackoverflow.com/questions/3490913/what-is-a-sticky-broadcast – Mushahid Gillani Apr 14 '20 at 10:46
1
TextView BatTemp;

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent intent) 
        {

          // TODO Auto-generated method stub

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

      };

@Override
      public void onCreate(Bundle b) 
      {
        super.onCreate(b);
        setContentView(R.layout.activity_main);


        BatTemp = (TextView) this.findViewById(R.id.textView8);

        this.registerReceiver(this.mBatInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
      }
Mohamad Bdour
  • 450
  • 5
  • 13
0

Try reading the static int BatteryManager.EXTRA_TEMPERATURE

GôTô
  • 7,974
  • 3
  • 32
  • 43
  • According to the documentation EXTRA_TEMPERATURE happens to be a String. It's also static and therefore will always be "temperature". – Christian Oct 22 '10 at 14:11
  • True, but the doc also says: "Extra for ACTION_BATTERY_CHANGED" - so your static String is just the key to get the temperature from ACTION_BATTERY_CHANGED. – Select0r Oct 22 '10 at 14:28