문제

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