Android 获取手机充电状态
2017-11-04 14:45
686 查看
通过注册广播
private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d(TAG, "action is " + action); switch (action) { case Intent.ACTION_POWER_CONNECTED: case "test.charge": mCharge = true; break; case Intent.ACTION_POWER_DISCONNECTED: case "test.uncharge": mCharge = false; break; case Intent.ACTION_BATTERY_CHANGED: mBatteryPercent = intent.getIntExtra("level", 100); int state = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); Log.d(TAG, " battery state is " + state); mCharge = (state == BatteryManager.BATTERY_STATUS_FULL) || (state == BatteryManager.BATTERY_STATUS_CHARGING); mLowerPower = mBatteryPercent <= mLowBatteryWarningPercent ? true : false; /* when power charging ,the lowerpower flag false*/ if (mCharge) { mLowerPower = false; } break;
通过注册
Intent.ACTION_POWER_CONNECTED
Intent.ACTION_POWER_DISCONNECTED
来获取手机插拔数据线状态。
通过注册
Intent.ACTION_BATTERY_CHANGED
来监听电量变化
case Intent.ACTION_BATTERY_CHANGED: mBatteryPercent = intent.getIntExtra("level", 100); int state = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); Log.d(TAG, " battery state is " + state); mCharge = (state == BatteryManager.BATTERY_STATUS_FULL) || (state == BatteryManager.BATTERY_STATUS_CHARGING);
通过
int state = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
来得到state 状态
state == BatteryManager.BATTERY_STATUS_CHARGING 判断是否充电状态。
相关文章推荐
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- Android 实时获取手机充电状态
- android 手机应用设备管理激活状态,获取激活状态,监听激活状态
- Android 如何判断手机处于电源充电状态还是USB连接状态?
- android-获取手机电话的状态
- TelephonyManager类:Android手机及Sim卡状态的获取
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android 获取手机状态栏高度
- TelephonyManager类:Android手机及Sim卡状态的获取
- Android如何获取当前手机的网络状态
- Android_程序判断手机ROOT状态,获取ROOT权限
- android 获取手机网络状态
- android 获取手机的各种状态
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- TelephonyManager类:Android手机及Sim卡状态的获取
- 获取Android手机中SIM卡状态
- android 获取手机网络状态
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动