|
|
如题,我用c语言写了一个安卓上的小程序。
作用就是后台运行 当
发现手机长按音量加 或 音量减 一秒后 触发自定义命令 比如reboot covery 重启进入回复模式。
现在把代码分享一下
先是头文件a.h
struct RawEvent {
int32_t when;
int32_t deviceId;
int32_t type;
int32_t code;
int32_t value;
};
void handle_exit(int sig)
{
system("reboot recovery");
}
int is_power_down_keya(int type, int code, int value)
{
int is_down = 0;
is_down = (type == 1) && (code == 114) && (value == 1);
return is_down;
}
int is_power_up_keya(int type, int code, int value)
{
int is_up = 0;
is_up = (type == 1) && (code == 114) && (value == 0);
return is_up;
}
int is_power_down_keyb(int type, int code, int value)
{
int is_down = 0;
is_down = (type == 1) && (code == 115) && (value == 1);
return is_down;
}
int is_power_up_keyb(int type, int code, int value)
{
int is_up = 0;
is_up = (type == 1) && (code == 115) && (value == 0);
return is_up;
}
然后是p.c文件
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/syslog.h>
#include <sys/time.h>
#include <sys/epoll.h>
#include <linux/input.h>
#include <sys/eventfd.h>
#include <stddef.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include "a.h"
//#include <unistd.h>
int main(int argc, char *argv[])
{
struct itimerval old_value;
int tap_power_time = 0;
int tap_power_type = 0;
int tap_power_code = 0;
int tap_power_value = 0;
struct itimerval tick;
int mEpollFd;
const int EPOLL_SIZE_HINT = 8;
const char* DEV_INPUT = "/dev/input/event4";
struct epoll_event mPendingEventItems[8];
unsigned char buf[1024] = {0};
struct inotify_event *event = NULL;
struct RawEvent* readBuffer = (struct RawEvent*) malloc (sizeof(struct RawEvent));
mEpollFd = epoll_create(EPOLL_SIZE_HINT);
int fd = open(DEV_INPUT, O_RDWR | O_CLOEXEC);
struct epoll_event eventItem;
memset(&eventItem, 0, sizeof(eventItem));
eventItem.events = EPOLLIN;
if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem) == -1)
{
exit(0);
}
for (;;)
{
epoll_wait(mEpollFd, mPendingEventItems, 8, -1);
int32_t readSize = read(fd, readBuffer, sizeof(struct input_event));
struct input_event *event =(struct input_event*) readBuffer;
tap_power_time = (int) event->time.tv_sec;
tap_power_type = event->type;
tap_power_code = event->code;
tap_power_value = event->value;
if(is_power_down_keya(tap_power_type, tap_power_code, tap_power_value))
{
signal(SIGALRM,handle_exit);
memset(&tick,0,sizeof(tick));
tick.it_value.tv_sec= 1;
tick.it_value.tv_usec= 0;
setitimer(ITIMER_REAL,&tick,NULL);
}
else if(is_power_up_keya(tap_power_type, tap_power_code, tap_power_value))
{
tick.it_value.tv_sec= 0;
tick.it_value.tv_usec= 0;
setitimer(ITIMER_REAL,&tick,NULL);
}
if(is_power_down_keyb(tap_power_type, tap_power_code, tap_power_value))
{
signal(SIGALRM,handle_exit);
memset(&tick,0,sizeof(tick));
tick.it_value.tv_sec= 1;
tick.it_value.tv_usec= 0;
setitimer(ITIMER_REAL,&tick,NULL);
}
else if(is_power_up_keyb(tap_power_type, tap_power_code, tap_power_value))
{
tick.it_value.tv_sec= 0;
tick.it_value.tv_usec= 0;
setitimer(ITIMER_REAL,&tick,NULL);
}
}
epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, &eventItem);
return 0;
}
编译直接安卓上跑ubuntu
用gcc -static p.c 即可
大家可以修改一下 reboot recovery 这里来自定义自己需要的功能
按键也可以修改下
还有就是 不同的手机 /dev/input/event不一样
比如我的 天玑820手机 是/dev/input/event1
但是骁龙870的另一个手机是/dev/input/event4
这个无关紧要,编译好的程序 自己用winhex 或sed
将event1 改成event2 event3 event4 直到能用就行
|
|