Вопрос

I've been creating a Battery Status on Android, but the Temperature and the Voltage doesn't come with the Battery Manager, for Example:

I need 28.7ºC, but Battery Manager get 287ºC, in Voltage I need 4.357, but it get 4357, so how do i get this number and insert the point that I need? Thanks!

My code is:

int temperature = intent.getIntExtra("temperature", -1);
int voltage = intent.getIntExtra("voltage", -1);
Это было полезно?

Решение

You can use something like this:

double temperature = intent.getIntExtra("temperature", -1) / 10;
double voltage = intent.getIntExtra("voltage", -1) / 1000;

As per this discussion, unit of measurement are "Tenth of centigrade" and "Millivolts" respectively for temperature and voltage in BatteryManager

Другие советы

You can do this by DecimalFormat

DecimalFormat df = new DecimalFormat("#.00"); // Set your desired format here.
System.out.println(df.format(temperature/10.0)); // Testing result

int variables doesn't support floating point. You need to use either double or float variables.

Well, then divide them:

float temperature = intent.getIntExtra("temperature", -1)/10;
float voltage = intent.getIntExtra("voltage", -1)/1000;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top