逻辑:避免电池长时间处于满电状态,以延缓电池老化速度和延长电池使用寿命。
1.处于设定的时间,一般是深夜(0点到4点),当电量到80%以上时,断开充电,4点后会恢复充电并直到充满
恢复充电, 弹出通知条件:1.暂停充电 2.充电线还连接 3.电量到80%
判断1: 监听广播 Intent.ACTION_POWER_CONNECTED 和 Intent.ACTION_POWER_DISCONNECTED ,实现电线是否插拔
也可监听Intent.ACTION_DOCK_EVENT 确定当前插接状态 developer.android.google.cn/training/mo…
判断2: FileReader读 FileOutputStream写充电节点的值,实现判断暂停充电,和开启关闭充电
判断3: 监控Intent.ACTION_BATTERY_CHANGED电池电量和充电状态 developer.android.google.cn/training/mo…
判断4: Intent.ACTION_TIME_CHANGED 和 Intent.ACTION_TIMEZONE_CHANGED 实现时间和时区改变
定时闹钟发自定义广播ChargingEvent , 监听ChargingEvent 判断时间到了或者电量等是否需要继续充电:
private void registerAlarmClock() {
Intent intent = new Intent();
intent.setAction(ChargingEvent);
intent.putExtra("type", INTELLIGENCE_TIME_CHANGED);
pendingIntent = PendingIntent.getBroadcast(mContext,101,
intent, PendingIntent.FLAG_IMMUTABLE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + getDelayTime(), pendingIntent);
}