public class OBatteryManager extends Manager {
private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
private ArrayList<BatteryStatusListener> mBatteryStatusListeners;
public OBatteryManager(Config config) {
super(config);
mBatteryStatusListeners = new ArrayList<>();
}
public void addBatteryStatusListener(BatteryStatusListener listener) throws Exception {
for (BatteryStatusListener l : mBatteryStatusListeners) {
if (l == listener) {
throw new Exception("重复添加BatteryStatusListener!");
}
}
mBatteryStatusListeners.add(listener);
}
public void removeBatteryStatusListener(BatteryStatusListener listener) {
for (BatteryStatusListener l : mBatteryStatusListeners) {
if (l == listener) {
mBatteryStatusListeners.remove(l);
}
}
}
/**
* 电源Manager的注册(register)和注销(unregister())必须在App中成对使用,且只能成对使用一次。
*/
public void register() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
intentFilter.addAction(BatteryManager.ACTION_CHARGING);
intentFilter.addAction(BatteryManager.ACTION_DISCHARGING);
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver();
SDKUtil.getAppContext().registerReceiver(mBatteryBroadcastReceiver, intentFilter);
}
/**
* 电源Manager的注册(register)和注销(unregister())必须在App中成对使用,且只能使用一次。
*/
public void unregister() {
SDKUtil.getAppContext().unregisterReceiver(mBatteryBroadcastReceiver);
}
/**
* 监听电量变化的广播。
*/
private class BatteryBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Detail detail = new Detail();
switch (action) {
case BatteryManager.ACTION_DISCHARGING:
try {
detail.data().put(BatteryManager.ACTION_DISCHARGING, intent.getBooleanExtra(BatteryManager.ACTION_DISCHARGING, false));
} catch (Exception e) {
e.printStackTrace();
}
break;
case BatteryManager.ACTION_CHARGING:
try {
detail.data().put(BatteryManager.ACTION_CHARGING, intent.getBooleanExtra(android.os.BatteryManager.ACTION_CHARGING, false));
} catch (Exception e) {
e.printStackTrace();
}
break;
case Intent.ACTION_BATTERY_CHANGED:
for (BatteryStatusListener listener : mBatteryStatusListeners) {
try {
extra(intent, detail);
} catch (Exception e) {
e.printStackTrace();
}
listener.batteryChanged(detail);
}
break;
case Intent.ACTION_BATTERY_LOW:
for (BatteryStatusListener listener : mBatteryStatusListeners) {
listener.batteryLow();
}
break;
case Intent.ACTION_BATTERY_OKAY:
for (BatteryStatusListener listener : mBatteryStatusListeners) {
listener.batteryOkay();
}
break;
case Intent.ACTION_POWER_CONNECTED:
for (BatteryStatusListener listener : mBatteryStatusListeners) {
listener.powerConnected();
}
break;
case Intent.ACTION_POWER_DISCONNECTED:
for (BatteryStatusListener listener : mBatteryStatusListeners) {
listener.powerDisConnected();
}
break;
}
}
}
/**
* 解析出更多电池信息装入Detail数据块中。
*
* @param intent
* @return
* @throws Exception
*/
private Detail extra(Intent intent, Detail detail) throws Exception {
//获取电池所有的附加信息。
Bundle extras = intent.getExtras();
//获取当前电量
int level = extras.getInt(BatteryManager.EXTRA_LEVEL, 0);
detail.data().put(BatteryManager.EXTRA_LEVEL, level);
//总电量
int scale = extras.getInt(BatteryManager.EXTRA_SCALE, 100);
detail.data().put(BatteryManager.EXTRA_SCALE, scale);
//电池温度
int temperature = extras.getInt(BatteryManager.EXTRA_TEMPERATURE);
detail.data().put(BatteryManager.EXTRA_TEMPERATURE, temperature);
//电池状态
//1,未知状态。 android.os.BatteryManager.BATTERY_STATUS_UNKNOWN
//2,充电。 android.os.BatteryManager.BATTERY_STATUS_CHARGING
//3,放电。 android.os.BatteryManager.BATTERY_STATUS_DISCHARGING
//4,未充电,包括放电和充满。android.os.BatteryManager.BATTERY_STATUS_NOT_CHARGING
//5,充满。 android.os.BatteryManager.BATTERY_STATUS_FULL
int status = extras.getInt(BatteryManager.EXTRA_STATUS);
detail.data().put(BatteryManager.EXTRA_STATUS, status);
// 电池健康程度
// 2,健康状态Good。android.os.BatteryManager.BATTERY_HEALTH_GOOD
// 3,过热。android.os.BatteryManager.BATTERY_HEALTH_OVERHEAT
// 5,电压过高。android.os.BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE
// 7,过冷。android.os.BatteryManager.BATTERY_HEALTH_COLD
int health = extras.getInt(BatteryManager.EXTRA_HEALTH);
detail.data().put(BatteryManager.EXTRA_HEALTH, health);
//当前电压。
detail.data().put(BatteryManager.EXTRA_VOLTAGE, extras.getInt(BatteryManager.EXTRA_VOLTAGE));
/**
* 充电方式。
* BATTERY_PLUGGED_AC = 1; 表示充电器
* BATTERY_PLUGGED_USB = 2; 表示USB
* BATTERY_PLUGGED_WIRELESS = 4; 表示无线
*/
detail.data().put(BatteryManager.EXTRA_PLUGGED, extras.getInt(BatteryManager.EXTRA_PLUGGED));
/**
*
* 当前电池技术。比如返回Li-poly表示锂电池。
*/
detail.data().put(BatteryManager.EXTRA_TECHNOLOGY, extras.getString(BatteryManager.EXTRA_TECHNOLOGY));
return detail;
}
}
---------------------
作者:zhangphil
来源:CSDN
原文:https://blog.csdn.net/zhangphil/article/details/89671624
版权声明:本文为博主原创文章,转载请附上博文链接!
android电量状态获取
最新推荐文章于 2024-07-07 11:09:08 发布