下面列出了android.os.BatteryManager#getIntProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String getBatteryStatus(Context context) {
if(Build.VERSION.SDK_INT < 26) {
return "not supported";
}
try {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if(bm != null) {
int status = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS);
switch (status) {
case BatteryManager.BATTERY_STATUS_CHARGING: return "charging";
case BatteryManager.BATTERY_STATUS_DISCHARGING: return "discharging";
case BatteryManager.BATTERY_STATUS_FULL: return "full";
case BatteryManager.BATTERY_STATUS_NOT_CHARGING: return "not charging";
case BatteryManager.BATTERY_STATUS_UNKNOWN: return "unknown";
default: return ""+status;
}
}
} catch (Exception e) {
if(Const.DEBUG) e.printStackTrace();
}
return "undefined";
}
/**
* Get the battery capacity at the moment (in %, from 0-100)
*
* @param context Application context
* @return Battery capacity (in %, from 0-100)
*/
public static int getBatteryCapacity(final Context context) {
int value = 0;
BatteryManager manager = (BatteryManager)
context.getSystemService(Context.BATTERY_SERVICE);
if (manager != null) {
value = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
}
if (value != 0 && value != Integer.MIN_VALUE) {
return value;
}
return 0;
}
/**
* Calculate Average Power
* Average Power = (Average Voltage * Average Current) / 1e9
*
* @param context Context of application
* @return Average power in integer
*/
public static int getBatteryAveragePower(final Context context) {
int voltage;
int current = 0;
Intent receiver =
context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if (receiver == null) return -1;
voltage = receiver.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
BatteryManager manager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if (manager != null) {
current = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
}
return (voltage * current) / 1000000000;
}
public static int getBatteryLevel(Context context) {
if(Build.VERSION.SDK_INT >= 21) {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if (bm != null) {
try {
return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
} catch (Exception e) {
if(Const.DEBUG) e.printStackTrace();
}
}
}
return -1;
}
public Event(Context context) {
this.deviceId = DeviceIdentifier.id(context);
this.time = new GregorianCalendar().getTime();
this.eventId = UUID.randomUUID().toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
BatteryManager bm = (BatteryManager)context.getSystemService(BATTERY_SERVICE);
this.batteryLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
} catch (Exception e) {
Log.e("TEST", e.getMessage());
}
}
}
public static EventChargingState grabChargingState(Context context) {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if (bm == null)
return new EventChargingState(false);
int status = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL;
EventChargingState event = new EventChargingState(isCharging);
return event;
}
private long getBatteryPercentageIfApi21() {
if(Build.VERSION.SDK_INT >= 21) {
BatteryManager bm = (BatteryManager) getSystemService(BATTERY_SERVICE);
return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
} else {
return -1;
}
}
/**
* Get the battery full capacity (charge counter) in mAh.
* Since Power (W) = (Current (A) * Voltage (V)) <=> Power (Wh) = (Current (Ah) * Voltage (Vh)).
* Therefore, Current (mA) = Power (mW) / Voltage (mV)
*
* @param context Application context
* @return Battery full capacity (in mAh)
*/
public static int getBatteryChargeCounter(final Context context) {
int value = 0;
BatteryManager manager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if (manager != null) {
value = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
}
return value;
// if (value <= 0) {
// value = getBatteryPropertyLegacy(Config.BATTERY_CHARGE_FULL);
// }
//
// if (value != 0 && value != Integer.MIN_VALUE) {
// return value;
// } else {
// // in uAh
// int chargeFullDesign =
// getBatteryPropertyLegacy(Config.BATTERY_ENERGY_FULL_DESIGN) / 1000000;
// int chargeFull = chargeFullDesign != 0 ?
// chargeFullDesign :
// getBatteryPropertyLegacy(Config.BATTERY_ENERGY_FULL) / 1000000;
//
// // in mAh
// return (chargeFull != 0) ? chargeFull : -1;
// }
}
public static int getBatteryCurrentAverage(final Context context) {
int value = 0;
BatteryManager manager = (BatteryManager)
context.getSystemService(Context.BATTERY_SERVICE);
if (manager != null) {
value = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
}
return (value != 0 && value != Integer.MIN_VALUE) ? value : 0;
}
/**
* Get the Battery current at the moment (in mA)
*
* @param context Application context
* @return battery current now (in mA)
*/
public static int getBatteryCurrentNow(final Context context) {
int value = 0;
BatteryManager manager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if (manager != null) {
value = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
}
return (value != 0 && value != Integer.MIN_VALUE) ? value : 0;
}
/**
* Get the Battery current at the moment (in mA)
*
* @param context Application context
* @return battery current now (in mA)
*/
public static double getBatteryCurrentNowInAmperes(final Context context) {
int value = 0;
BatteryManager manager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if (manager != null) {
value = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
}
value = (value != 0 && value != Integer.MIN_VALUE) ? value : 0;
return (double) value / 1000000;
}
/**
* Calculate Battery Capacity Consumed
* Battery Capacity Consumed = (Average Current * Workload Duration) / 1e3
*
* @param workload Workload duration (in hours)
* @param context Context of application
* @return Average power in integer
*/
public static double getBatteryCapacityConsumed(final double workload, final Context context) {
int current = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
BatteryManager manager = (BatteryManager)
context.getSystemService(Context.BATTERY_SERVICE);
current = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
}
return (current * workload) / 1000;
}
@Override
public void handlePUT(CoapExchange ce) {
// Split on & and = then on ' '
String outLine = "";
byte[] payload = ce.getRequestPayload();
String inputLine = new String(payload);
int AUAVsim = 0;
if (inputLine.contains("dp=AUAVsim")) {
AUAVsim = 1;
}
String[] args = inputLine.split("-");//???
switch (args[0]) {
case "dc=help":
ce.respond(getUsageInfo());
break;
case "dc=qry":
String qry = args[1].substring(3);
ce.respond(queryH2(qry));
break;
case "dc=dji":
System.out.println("Battery Value is: " + djiLastReading);
System.out.println("Battery MAH is: " + djiLastMAH);
System.out.println("Battery Current is: "+djiCurrent);
System.out.println("Battery Voltage is: "+djiVoltage);
ce.respond("Percent=" + djiLastReading+", MAH="+djiLastMAH);
case "dc=lcl":
if (AUAVsim == 1) {
lclLastReading--;
addReadingH2(lclLastReading, "lcl");
ce.respond("Battery: " + Integer.toString(lclLastReading));
break;
}
try {
Class<?> c = Class.forName("android.app.ActivityThread");
android.app.Application app =
(android.app.Application) c.getDeclaredMethod("currentApplication").invoke(null);
android.content.Context context = app.getApplicationContext();
BatteryManager bm = (BatteryManager)context.getSystemService("batterymanager");
int batLevel = bm.getIntProperty(4);
lclLastReading = batLevel;
addReadingH2(batLevel, "lcl");
ce.respond("Battery: " + Integer.toString(batLevel));
}
catch (Exception e) {
ce.respond("Battery: Error");
}
break;
case "dc=cfg":
if(AUAVsim != 1){
initBatteryCallback();
}
ce.respond("Battery: Configured");
default:
ce.respond("Error: BatteryDriver unknown command\n");
}
}
@TargetApi(Build.VERSION_CODES.M)
private Drawable getBatteryDrawable() {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
if(batLevel == Integer.MIN_VALUE)
return null;
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
String batDrawable;
if(batLevel < 10 && !isCharging)
batDrawable = "alert";
else if(batLevel < 25)
batDrawable = "20";
else if(batLevel < 40)
batDrawable = "30";
else if(batLevel < 55)
batDrawable = "50";
else if(batLevel < 70)
batDrawable = "60";
else if(batLevel < 85)
batDrawable = "80";
else if(batLevel < 95)
batDrawable = "90";
else
batDrawable = "full";
String charging;
if(isCharging)
charging = "charging_";
else
charging = "";
String batRes = "tb_battery_" + charging + batDrawable;
int id = getResourceIdFor(batRes);
return getDrawableForSysTray(id);
}
public static int getBatteryLevel(Context context) {
BatteryManager bm = (BatteryManager)context.getSystemService(BATTERY_SERVICE);
return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
}