安卓获取电池信息

总结了一些代码,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
个人分类: 移动开发
上一篇记录一些常用git命令,省得老google
下一篇Android NDK开发技术与技巧总结
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭