在android中,我使用sendevent来模拟点击和拖动事件。代码是:
String[] events = new String[7];
events[0] = "sendevent /dev/input/event1 3 57 0";
events[1] = "sendevent /dev/input/event1 3 53 " + x1;
events[2] = "sendevent /dev/input/event1 3 54 " + y1;
events[3] = "sendevent /dev/input/event1 3 58 31";
events[4] = "sendevent /dev/input/event1 3 50 2";
events[5] = "sendevent /dev/input/event1 0 2 0";
events[6] = "sendevent /dev/input/event1 0 0 0";
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
for (String event : events) {
os.writeBytes(event + "\n");
os.flush();
}
是的,它可以达到结果,但是它非常慢且不稳定,每个sendevent都会新一个进程来执行它。
然后我在https://www.assembla.com/code/android-gb-for-sharp-is01/git/nodes/debug/system/core/toolbox/sendevent.c中找到 sendevent 源,它由 c 编写,并且每次它会打开一个文件并写入args,它每次都会创建一个新进程,所以我有一个想法,将sendevent重写为Java并直接在android代码中使用,它只需要打开一次文件并写入所有args .
可行吗?它可以加速我的代码吗?