关闭Android电池温度告警框,android电池温度报警

基于mtk平台的电池温度报警

基本思路:循环读取设备文件,读取到温度值,发送广播给上层应用,广播收到温度值后UI提示。代码如下:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "battery_warning",__VA_ARGS__)

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , "battery_warning",__VA_ARGS__)

#define LOGI(...) __android_log_print(ANDROID_LOG_INFO , "battery_warning",__VA_ARGS__)

#define LOGW(...) __android_log_print(ANDROID_LOG_WARN , "battery_warning",__VA_ARGS__)

#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , "battery_warning",__VA_ARGS__)

#define MAX_CHAR 100

#define MAX_LENGTH 1024

#define CMD "ps -t | grep ActivityManager"

#define FILE_NAME "/sys/devices/platform/mt-battery/BatteryNotify"

#define INTENT "am broadcast -n com.mediatek.batterywarning/com.mediatek.batterywarning.BatteryWarningReceiver -a android.intent.action.BATTERY_WARNING --ei type "

int 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.mk文件

#---------------------------------

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := batterywarning.c

LOCAL_SHARED_LIBRARIES := libc libcutils

LOCAL_MODULE := batterywarning

LOCAL_MODULE_TAGS := optional

include $(BUILD_EXECUTABLE)

#----------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值