Get Temperature of Battery on Android

Get temperature of battery on android

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";

How get battery temperature with decimal?

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.

how to get battery temperature in android

You can find here how to register to battery changes. Using the extra value (EXTRA_TEMPERATURE) you can get the temperature.

In short (taken from the link above and modified a bit):

Declare broadcast receiver:

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
....
}
};

and in onCreate, add:

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

Getting the Battery temperature and unit of the result

The temperature is in tenths of a degree centigrade. Also discussed in question here.

How to get battery temperature from Battery Manager Class in android?

BroadcastReceiver is an abstract class. This means that you cannot make an instance of it. You have to create your own class and extend BroadcastReceiver.

I think you can copy all of your existing code into your new class.

You seem to have 3 Broadcasters.
The first one shoudl look like this:

public class BatInfoReceiver extends BroadcastReceiver{
@Override
public void onRecieve(Context arg0, Intent intent){
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
BatTemp.setText(String.valueOf(temp) + "%");

}
}

Now its possible to make an instance of this class:

BatInforeceiver mBatInfoReceiver = new BatInfoReceiver();

And then you do the same with your other two BroadcastReceivers.

Get temperature of battery on android

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";

Android Hardware Battery temperature, where is the sensor?

In theory, yes, but the battery temperature is critical in the charge/discharge cycle of the phone. Phone batteries charge by applying a constant current to the battery. When the phone is charging the battery temp will rise (a byproduct of mixing electricity with chemicals). If it rises too far, the battery will fail (and could [in theory at least] explode).

Thus the phone's charging circuitry needs to monitor the battery temp so if it rises beyond a given threshold it backs the charging current off until the temperature stablises, ensuring the battery remains within safe limits.

In other words -- if you do override it, do so at your own risk.



Related Topics



Leave a reply



Submit