电池报警来源 和 电池和电量关机设置修改
基本思路:循环读取设备文件,读取到温度值,发送广播给上层应用,广播收到温度值后UI提示
nt systemServerStarted(const char *cmd)
{
LOGD(“systemServerStarted: %s \n”, cmd);
int res = 0;
char buf_ps[MAX_LENGTH];
char ps[MAX_LENGTH]={0};
FILE *ptr;
strcpy(ps, cmd);
if((ptr=popen(ps, “r”)) != NULL)
{
while(fgets(buf_ps, MAX_LENGTH, ptr) != NULL) //通过ps命令查找ActivityManager是否存在
{
LOGD(“cmd result: %s”, buf_ps);
if(strlen(buf_ps) > 0)
{
res = 1;
break;
}
}
pclose(ptr);
ptr = NULL;
}
else
{
LOGE(“popen error”);
}
return res;
}
void readType(char* buffer) {
FILE * pFile;
pFile = fopen(FILE_NAME, “r”); //打开设备文件
if(pFile == NULL) {
LOGE(“error opening file”);
return;
} else {
if(fgets(buffer, MAX_CHAR, pFile) == NULL) {
LOGE(“can not get the string from the file”);
return;
}
}
int type = atoi(buffer); //转化为整形
if (type > 0 && systemServerStarted(CMD))
{
LOGD(“start activity by send intent to BatteryWarningReceiver, type = %s”, buffer);
char ps[MAX_LENGTH] = INTENT;
strcat(ps,buffer);
LOGD(“os.system: %s”, ps);
system(ps); //发送广播附件电池温度,给上层应用
}
fclose(pFile);
}
int main(int argc, char **argv)
{
char buffer = (char) malloc(MAX_CHAR * sizeof(char));
while(1) {
readType(buffer); //读取设备文件中温度值
sleep(10);
}
free(buffer);
return 0;
}
Android系统默认是电量为0关机的
/frameworks/base/services/java/com/android/server/BatteryService.java
电池这一块自然少不了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,修改后低电关机相关的代码如下:
mBatteryProps.batteryLevel 电量
mBatteryProps.batteryTemperature 电池温度
mShutdownBatteryTemperature Android默认设置值680 在framworks中cord/res/velues/config设置关机温度
推荐阅读