How do I get the temperature of the battery in android?
Get temperature of battery on android
Asked Answered
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";
What's better about this solution than the much shorter one: https://mcmap.net/q/472097/-get-temperature-of-battery-on-android –
Ludicrous
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.
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 broken –
Marbling
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";
What's better about this solution than the much shorter one: https://mcmap.net/q/472097/-get-temperature-of-battery-on-android –
Ludicrous
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";
}
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-broadcast –
Isom
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));
}
Try reading the static int BatteryManager.EXTRA_TEMPERATURE
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.