当前位置: 首页 > news >正文

Android 13.0 系统framework修改低电量关机值为3%

1、讲在最前面

系统rom定制开发中,其中在低电量时,系统会自动关机,这个和不同的平台和底层驱动和硬件都有关系,需要结合这些来实际调整这个值,我们可以通过分析源码中电池服务的代码,然后进行修改如何实现这个功能。

2、系统中framework修改低电量关机值的核心类

frameworks\base\services\core\java\com\android\server\BatteryService.java
    private final class HealthHalCallback extends IHealthInfoCallback.Stubimplements HealthServiceWrapper.Callback {@Override public void healthInfoChanged(android.hardware.health.V2_0.HealthInfo props) {BatteryService.this.update(props);}private void update(android.hardware.health.V2_0.HealthInfo info) {traceBegin("HealthInfoUpdate");Trace.traceCounter(Trace.TRACE_TAG_POWER, "BatteryChargeCounter",info.legacy.batteryChargeCounter);Trace.traceCounter(Trace.TRACE_TAG_POWER, "BatteryCurrent",info.legacy.batteryCurrent);synchronized (mLock) {mRealBatteryLevel = info.legacy.batteryLevel;if (!mUpdatesStopped) {mHealthInfo = info.legacy;// Process the new values.processValuesLocked(false);mLock.notifyAll(); // for any waiters on new info} else {copy(mLastHealthInfo, info.legacy);}}traceEnd();}private void processValuesLocked(boolean force) {boolean logOutlier = false;long dischargeDuration = 0;mBatteryLevelCritical =mHealthInfo.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN&& mHealthInfo.batteryLevel <= mCriticalBatteryLevel;if (mHealthInfo.chargerAcOnline) {mPlugType = BatteryManager.BATTERY_PLUGGED_AC;} else if (mHealthInfo.chargerUsbOnline) {mPlugType = BatteryManager.BATTERY_PLUGGED_USB;} else if (mHealthInfo.chargerWirelessOnline) {mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;} else {mPlugType = BATTERY_PLUGGED_NONE;}if (DEBUG) {Slog.d(TAG, "Processing new values: "+ "info=" + mHealthInfo+ ", mBatteryLevelCritical=" + mBatteryLevelCritical+ ", mPlugType=" + mPlugType);}// Let the battery stats keep track of the current level.try {mBatteryStats.setBatteryState(mHealthInfo.batteryStatus, mHealthInfo.batteryHealth,mPlugType, mHealthInfo.batteryLevel, mHealthInfo.batteryTemperature,mHealthInfo.batteryVoltage, mHealthInfo.batteryChargeCounter,mHealthInfo.batteryFullCharge);} catch (RemoteException e) {// Should never happen.}...
}private void shutdownIfNoPowerLocked() {// shut down gracefully if our battery is critically low and we are not powered.// wait until the system has booted before attempting to display the shutdown dialog.if (shouldShutdownLocked()) {/*SPRD : add power debug log start*/Slog.d(TAG, "Low battery shutdown, batteryLevel : " + mHealthInfo.batteryLevel);/*SPRD : add power debug log end*/mHandler.post(new Runnable() {@Overridepublic void run() {if (mActivityManagerInternal.isSystemReady()) {Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);intent.putExtra(Intent.EXTRA_REASON,PowerManager.SHUTDOWN_LOW_BATTERY);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);mContext.startActivityAsUser(intent, UserHandle.CURRENT);}}});}}private boolean shouldShutdownLocked() {if (mHealthInfo.batteryLevel > 0) {return false;}// Battery-less devices should not shutdown.if (!mHealthInfo.batteryPresent) {return false;}// add for bug#1021541boolean checkPlugState = (mUpdatesStopped && mHealthInfo.batteryLevel == mSetBatteryLevel&& mSetBatteryLevel != mRealBatteryLevel);if (DEBUG) Slog.d(TAG, "shutdownIfNoPowerLocked: mUpdatesStopped: " + mUpdatesStopped+ " mHealthInfo.batteryLevel:"  + mHealthInfo.batteryLevel+ " mSetBatteryLevel:" + mSetBatteryLevel + " mRealBatteryLevel:" + mRealBatteryLevel);return !checkPlugState || mHealthInfo.batteryStatus != BatteryManager.BATTERY_STATUS_CHARGING;}private boolean shouldShutdownLocked() {
//add startif (mHealthInfo.batteryLevel <= 2) {return true;}//add endif (mHealthInfo.batteryLevel > 0) {return false;}// Battery-less devices should not shutdown.if (!mHealthInfo.batteryPresent) {return false;}// add for bug#1021541boolean checkPlugState = (mUpdatesStopped && mHealthInfo.batteryLevel == mSetBatteryLevel&& mSetBatteryLevel != mRealBatteryLevel);if (DEBUG) Slog.d(TAG, "shutdownIfNoPowerLocked: mUpdatesStopped: " + mUpdatesStopped+ " mHealthInfo.batteryLevel:"  + mHealthInfo.batteryLevel+ " mSetBatteryLevel:" + mSetBatteryLevel + " mRealBatteryLevel:" + mRealBatteryLevel);return !checkPlugState || mHealthInfo.batteryStatus != BatteryManager.BATTERY_STATUS_CHARGING;}

经过源码分析,如果满足shouldShutdownLocked(),如果满足关机执行 Intent.ACTION_REQUEST_SHUTDOWN
添加

   if (mHealthInfo.batteryLevel <= 2) {return true;}
http://www.lryc.cn/news/297493.html

相关文章:

  • 【EAI 013】BC-Z: Zero-Shot Task Generalization with Robotic Imitation Learning
  • 一文讲透ast.literal_eval() eval() json.loads()
  • 微软.NET6开发的C#特性——类、结构体和联合体
  • naiveui 上传图片遇到的坑 Upload
  • 安全之护网(HVV)、红蓝对抗
  • Leetcode 213 打家劫舍 II
  • 【C语言】三子棋游戏实现代码
  • docker常用10条容器操作命令
  • 《MySQL 简易速速上手小册》第2章:数据库设计最佳实践(2024 最新版)
  • 利用YOLOv8 pose estimation 进行 人的 头部等马赛克
  • 【Python 千题 —— 基础篇】查找年龄
  • 前后端通讯:前端调用后端接口的五种方式,优劣势和场景
  • Mysql大表添加字段失败解决方案
  • (52)只出现一次的数字III
  • Linux增删ip
  • 【计算机网络】时延,丢包,吞吐量(分组交换网络
  • 张楠辞任抖音集团CEO;东方甄选将开服饰号;小红书新增“附近”一级入口;华为分红770亿元
  • ES监控方法以及核心指标
  • 无人机应用场景和发展趋势,无人机技术的未来发展趋势分析
  • JavaGuide
  • IDEA创建SpringBoot+Mybatis-Plus项目
  • 第9章 SpringBoot综合项目实战——个人博客系统
  • 怎么理解 Redis 事务
  • react中的diff算法
  • 【医学大模型 尘肺病】PneumoLLM:少样本大模型诊断尘肺病新方法
  • 【SpringBootStarter】自定义全局加解密组件
  • 【射影几何15】python双曲几何工具geometry_tools
  • 机器人抓取 [ 题目/摘要 ] 更新中..
  • 【51单片机】外部中断和定时器中断
  • 零售行业供应商数据分发,怎样提高安全性和效率?