AndroidAPS/app/src/main/java/info/nightscout/utils/BatteryLevel.java

35 lines
1 KiB
Java
Raw Normal View History

2017-02-20 08:32:10 +01:00
package info.nightscout.utils;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
2017-02-21 13:45:33 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2017-02-20 08:32:10 +01:00
import info.nightscout.androidaps.MainApp;
/**
* Created by mike on 20.02.2017.
*/
public class BatteryLevel {
2017-02-21 13:45:33 +01:00
private static Logger log = LoggerFactory.getLogger(BatteryLevel.class);
2017-02-20 08:32:10 +01:00
static public int lastUploadedLevel = 0;
static public int getBatteryLevel() {
2017-02-21 13:45:33 +01:00
int batteryLevel = 0;
2017-02-20 08:32:10 +01:00
Intent batteryIntent = MainApp.instance().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if (batteryIntent != null) {
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
2017-02-21 13:45:33 +01:00
if (level != -1 && scale != -1) {
batteryLevel = (int) (((float) level / (float) scale) * 100.0f);
2017-02-20 08:32:10 +01:00
}
2017-02-21 13:45:33 +01:00
}
log.debug("Battery level: " + batteryLevel);
return batteryLevel;
2017-02-20 08:32:10 +01:00
}
}