码迷,mamicode.com
首页 > 其他好文 > 详细

充电状态和BatteryManager

时间:2019-08-25 18:23:49      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:att   wireless   dash   less   cti   one   com   imp   filter   

我们可以通过如下代码获取现在的充电状态是否为AC充电。

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, filter);(http://www.amjmh.com/v/BIBRGZ_558768/)
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
if (acCharge) {
Log.v(LOG_TAG,“The phone is charging!”);
}
1
2
3
4
5
6
7
得到充电的状态之后我们可以有针对性的做一些操作,比如在充电时执行一些耗时的代码。

private boolean checkForPower() {
// It is very easy to subscribe to changes to the battery state, but you can get the current
// state by simply passing null in as your receiver. Nifty, isn‘t that?
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, filter);

// There are currently three ways a device can be plugged in. We should check them all.
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB);
boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
boolean wirelessCharge = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
wirelessCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS);
}
return (usbCharge || acCharge || wirelessCharge);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
上面代码可以得到现在是否充电。
————————————————

充电状态和BatteryManager

标签:att   wireless   dash   less   cti   one   com   imp   filter   

原文地址:https://www.cnblogs.com/hyhy904/p/11408516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!