安卓获取电池信息

总结了一些代码,mark住。

private void test()
    {
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatusIntent = context.registerReceiver(null, intentFilter);

        // 是否在充电
        int chargeStatus = batteryStatusIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = chargeStatus == BatteryManager.BATTERY_STATUS_CHARGING ||
                chargeStatus == BatteryManager.BATTERY_STATUS_FULL;

        // 怎么充
        int chargePlug = batteryStatusIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        int quantity = batteryStatusIntent.getIntExtra("level", 0); 

        int voltage = batteryStatusIntent.getIntExtra("voltage", 0) / 1000;

        int temperature = batteryStatusIntent.getIntExtra("temperature",0) / 10;

        int health = batteryStatusIntent.getIntExtra("health",
                BatteryManager.BATTERY_HEALTH_UNKNOWN);
    }

你可能感兴趣的:(battery)