Android获取电池充电状态的方式
2017-10-19 16:12
507 查看
废话不多说,直接进入正题。
我需要知道当前设备是否在充电,如何实现?
1、注册电池更改广播ACTION_BATTERY_CHANGED接收器。实现代码如下:
二、直接注册充电状态广播接收器
其实上面两种方式归结起来是一种方式。仔细分析一下这个两个广播在发送时候,就知道原因了。
frameworks/base/services/core/java/com/android/server/BatteryService.java#sendIntentLocked()
我需要知道当前设备是否在充电,如何实现?
1、注册电池更改广播ACTION_BATTERY_CHANGED接收器。实现代码如下:
IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); context.registerReceiver(this, filter); .............. public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { int status=intent.getIntExtra("status",BatteryManager.BATTERY_STATUS_UNKNOWN); if(status==BatteryManager.BATTERY_STATUS_CHARGING) { //DoSomeThing } else { //DoSomeThing } } }
二、直接注册充电状态广播接收器
filter = new IntentFilter(); filter.addAction(BatteryManager.ACTION_CHARGING); filter.addAction(BatteryManager.ACTION_DISCHARGING); context.registerReceiver(this, filter); .............. public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(BatteryManager.ACTION_CHARGING)) { //DoSomething } else (action.equals(BatteryManager.ACTION_DISCHARGING)) { //DoSomething } }
其实上面两种方式归结起来是一种方式。仔细分析一下这个两个广播在发送时候,就知道原因了。
frameworks/base/services/core/java/com/android/server/BatteryService.java#sendIntentLocked()
// Pack up the values and broadcast them to everyone final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_REPLACE_PENDING); int icon = getIconLocked(mBatteryProps.batteryLevel); intent.putExtra(BatteryManager.EXTRA_STATUS, mBatteryProps.batteryStatus);//电池状态信息,可以在广播中直接获取当前电池的状态信息 intent.putExtra(BatteryManager.EXTRA_HEALTH, mBatteryProps.batteryHealth); intent.putExtra(BatteryManager.EXTRA_PRESENT, mBatteryProps.batteryPresent); intent.putExtra(BatteryManager.EXTRA_LEVEL, mBatteryProps.batteryLevel);//电量值 intent.putExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE); intent.putExtra(BatteryManager.EXTRA_ICON_SMALL, icon); intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);//充电类型 intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage); intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);//电池温度 intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology); intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger); intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
相关文章推荐
- Android获取电池充电状态的方式
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- android 获取充电状态 电池电量
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- 转: Android 电源管理专题之监测电池电量和充电状态Monitoring the Battery Level and Charging State
- unity3d 获取电量电池状态等 android and ios
- Android 优化电池使用时间 ——监控电池电量和充电状态
- Android:充电状态、轮询、电池状态、Notification
- Android:充电状态、轮询、电池状态、Notification
- Android获取屏幕状态的方式
- Android 的电池消耗优化 II-监测电量等级和充电状态
- Android监控电池电量和充电状态
- android 5.0 获取屏幕状态方式
- Android设备的电池续航时间优化(Optimizing Battery Life)——(一)监听电池电量和电池的充电状态
- 获取Android设备电池电量状态
- Android开发:优化电池续航-监听电量和充电状态
- Android 实时获取手机充电状态
- 【Android Training - Performance】优化电池续航能力[Lesson 1 - 监测设备的电量与充电状态]
- Android判断网络连接状态,判断网络连接方式,获取IP地址
- 获取Android设备电池电量状态