Get temperature of battery on android
Asked Answered
A

5

18

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

Antre answered 22/10, 2010 at 13:33 Comment(0)
Y
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";
Yurik answered 6/10, 2013 at 1:13 Comment(1)
What's better about this solution than the much shorter one: https://mcmap.net/q/472097/-get-temperature-of-battery-on-androidLudicrous
C
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.

Connote answered 22/10, 2010 at 13:40 Comment(5)
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?Antre
@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.Preliminaries
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 (#7716554)Synapse
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.Cotsen
@Connote link is brokenMarbling
Y
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";
Yurik answered 6/10, 2013 at 1:13 Comment(1)
What's better about this solution than the much shorter one: https://mcmap.net/q/472097/-get-temperature-of-battery-on-androidLudicrous
C
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";
    }
Californium answered 25/1, 2014 at 21:4 Comment(2)
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!Ludicrous
@CorneliusRoemer because it is a sticky intent/sticky broadcast, for more detail developer.android.com/training/monitoring-device-state/… https://mcmap.net/q/223730/-what-is-a-sticky-broadcastIsom
E
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));
      }
Ecclesiasticus answered 20/2, 2013 at 15:25 Comment(0)
C
0

Try reading the static int BatteryManager.EXTRA_TEMPERATURE

Christalchristalle answered 22/10, 2010 at 13:43 Comment(2)
According to the documentation EXTRA_TEMPERATURE happens to be a String. It's also static and therefore will always be "temperature".Antre
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.Connote

© 2022 - 2024 — McMap. All rights reserved.