Android 获取设备电池电量
2016-09-28 15:39
387 查看
注册广播接收器:
广播接收ACTION_BATTERY_CHANGED:
设置电池图标:
homeFilter = new IntentFilter(); homeFilter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(homeReceiver, homeFilter);
广播接收ACTION_BATTERY_CHANGED:
private BroadcastReceiver homeReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { int rawLevel = intent.getIntExtra("level", -1); int scale = intent.getIntExtra("scale", -1); int status = intent.getIntExtra("status", -1); int health = intent.getIntExtra("health", -1); MyLog.i("[HomeReceiver]rawLevel:" + rawLevel + ",scale:" + scale + ",status:" + status + ",health:" + health); switch (status) { case BatteryManager.BATTERY_STATUS_UNKNOWN: setBatteryImageByLevel(0); break; case BatteryManager.BATTERY_STATUS_CHARGING: break; case BatteryManager.BATTERY_STATUS_DISCHARGING: case BatteryManager.BATTERY_STATUS_NOT_CHARGING: if (rawLevel >= 0 && rawLevel <= 100) { int level = (int) (rawLevel / 10.0); setBatteryImageByLevel(level); } break; case BatteryManager.BATTERY_STATUS_FULL: setBatteryImageByLevel(10); break; default: break; } } } };
设置电池图标:
/** * 设置电池图标 * * @param level * 0-10 */ private void setBatteryImageByLevel(int level) { MyLog.i("[setBatteryImageByLevel]:" + level); if (level >= 0 && level <= 10) { int batteryResId = getResources().getIdentifier( "home_battery_" + level, "drawable", getPackageName()); imageBattery.setImageResource(batteryResId); } }
相关文章推荐
- 获取Android设备电池电量状态
- 获取Android设备电池电量状态
- 获取Android设备电池电量状态
- 获取Android设备电池电量状态
- native层获取Android设备电池电量
- 获取Android设备电池电量状态
- 获取Android设备电池电量状态
- Android获取设备电池信息,玩些新花样
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- android获取电池电量(无延迟)
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- Android中获取电池电量
- Android获取手机电池电量用法实例
- Android中获取电池电量
- Android应用开发获取手机电池电量的简单方法
- Android 获取电池电量
- Android获取电池电量的三种方法
- Android学习之获取 电池电量 简单实例
- Android设备的电池续航时间优化(Optimizing Battery Life)——(一)监听电池电量和电池的充电状态
- 获取设备电池电量及状态