修改Android关机电量和关机温度

http://blog.chinaunix.net/uid-26926660-id-3329353.html

Android系统默认是电量为0关机的,如果要修改成还有5%电量就关机怎么办?(吐槽一下:其实修改成5%关机也没什么意义,因为即便还有电量,开机后系统也会再次被关闭),不过确实有这样的需求,废话少说,这里简单分析怎么改:

 

2.分析

 

电池这一块自然少不了Android BatteryService,在adb shell中敲入:

dumpsys battery

输出如下:

Current Battery Service state:

  AC powered: false

  USB powered: true

  status: 2

  health: 2

  present: true

  level: 54

  scale: 100

  voltage:3856

  temperature: 300

  technology: LiFe

其中的level就是电量等级,temperature是摄氏温度,不过少了小数点,是30度。BatteyService中决定关机的就两个,一个level,一个temperature

 

mBatteryLevel,就是系统的电压等级,最大值是SCALE,也就是100,修改后低电关机相关的代码如下:

@./frameworks/base/services/java/com/android/server/BatteryService.java

  1. private final void shutdownIfNoPower() {
  2.         // shut down gracefully if our battery is critically low and we are not powered.
  3.         // wait until the system has booted before attempting to display the shutdown dialog.
  4.         if (mBatteryLevel < 5 && !isPowered() && ActivityManagerNative.isSystemReady()) {
  5.             Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
  6.             intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
  7.             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  8.             mContext.startActivity(intent);
  9.         }
  10.     }
  11.  
  12.     private final void shutdownIfOverTemp() {
  13.         // shut down gracefully if temperature is too high (> 68.0C)
  14.         // wait until the system has booted before attempting to display the shutdown dialog.
  15.         if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
  16.             Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
  17.             intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
  18.             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  19.             mContext.startActivity(intent);
  20.         }
  21.     }

关机的原理是通过发送关机对话框的Intent来实现的,而不是调用ShutdownThread或是4.1的PowerManager

来实现的,这里确实体现了Android的灵活之处。

    关于BatteryService参数的更新,目前知道是通过uevent机制和sysfs进行交互更新的,这一块还需要进一步跟进一下。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值