Java Code Examples for android.os.BatteryManager#getIntProperty()

The following examples show how to use android.os.BatteryManager#getIntProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Util.java    From android-notification-log with MIT License 7 votes vote down vote up
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";
}
 
Example 2
Source File: Event.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
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());
        }
    }
}
 
Example 3
Source File: Battery.java    From batteryhub with Apache License 2.0 7 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: Battery.java    From batteryhub with Apache License 2.0 7 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: Util.java    From android-notification-log with MIT License 6 votes vote down vote up
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;
}
 
Example 6
Source File: ChargingStateReceiver.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: SmsSenderService.java    From Locate-driver with GNU General Public License v3.0 6 votes vote down vote up
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;
    }
}
 
Example 8
Source File: Battery.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
/**
     * 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;
//        }
    }
 
Example 9
Source File: Battery.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 10
Source File: Battery.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: Battery.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: Battery.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: BatteryDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
@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");
		}
}
 
Example 14
Source File: TaskbarController.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source File: Utils.java    From ForceDoze with GNU General Public License v3.0 5 votes vote down vote up
public static int getBatteryLevel(Context context) {
    BatteryManager bm = (BatteryManager)context.getSystemService(BATTERY_SERVICE);
    return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
}