关闭

安卓获取电池信息

标签: battery
878人阅读 评论(0) 收藏 举报
分类:

总结了一些代码,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);
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:82768次
    • 积分:1418
    • 等级:
    • 排名:千里之外
    • 原创:49篇
    • 转载:0篇
    • 译文:0篇
    • 评论:82条
    文章分类
    最新评论