如何模拟触摸,从后台服务与的SendEvent或其他方式?
是否可以模拟从后台应用程序的触摸(或服务),或运行的sh脚本(即模拟触摸)?
Is it possible to simulate touch from the background application (or service) or to run sh script (that simulate touch)?
这是需要测试Android系统没有USB或PC等的连接,这就是为什么我不能(或者别知道如何)使用猴子或其他自动测试工具。
It is needed for testing android system without USB or other connection to PC, thats why I can't (or don' know how) use Monkey or other autotest tools.
补充信息: 我发现执行root shell命令(根植测试设备)的方式:
Added info: I found the way to run shell commands with root (tested devices rooted):
Unable通过Android code 执行的SendEvent shell命令(创建触控模拟)。 安卓:写在系统分区文件(以root权限运行命令)
Unable to execute sendevent shell command through the android code (create touch simulation). Android: Writing file on system partition (run commands with root permissions)
另外,我得到的事件来模拟触控。
Also I get events to simulate touch.
因此,我有:
//sendevent commands to simulate touch (verify it work from cmd)
String[] touchEvent = { "sendevent /dev/input/event0 0 0 0\n",
"sendevent /dev/input/event6 3 53 499\n",
"sendevent /dev/input/event6 3 54 680\n",
"sendevent /dev/input/event6 3 58 40\n",
"sendevent /dev/input/event6 3 48 3\n",
"sendevent /dev/input/event6 3 57 0\n",
"sendevent /dev/input/event6 0 2 0\n",
"sendevent /dev/input/event6 0 0 0\n",
"sendevent /dev/input/event6 0 2 0\n",
"sendevent /dev/input/event6 0 0 0\n",
"sendevent /dev/input/event0 3 0 2\n",
"sendevent /dev/input/event0 0 0 0\n"};
try{
Thread.sleep(2000);
Process root = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(root.getOutputStream());
for(int i = 0; i < touchEvent.length; i++){
Log.i(TAG, touchEvent[i]);
os.writeBytes(touchEvent[i]);
os.flush();
}
root.waitFor();
} catch (IOException e) {
Log.e(TAG, "Runtime problems\n");
e.printStackTrace();
} catch (SecurityException se){
se.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
我没有任何异常,但它不碰模拟。
I have no any exceptions, but it is not touch simulates.
任何人可以帮助解决这个问题?
Can anybody help to solve this problem?
如果有另一种方式与基于C的Android NDK或守护做到这一点,请告诉我一下吧。
If there is another way to do it with android ndk or daemon on C, please tell me about it.
感谢。
我无法执行的SendEvent命令,却发现另一种为自己,希望这将是有益的人。
I can't execute the "sendevent" command, but found another way for myself, hope it will be helpfull for somebody.
有关模拟触摸我用sendPointerSync()从android.app.Instrumentation,这项工作只能用android.permission.INJECT_EVENTS权限。为了使用它,你应该编译你的应用程序作为系统应用程序。 要做到这一点,你应该按照下面的步骤:
For simulate touch I used sendPointerSync() from android.app.Instrumentation, that work only with "android.permission.INJECT_EVENTS" permission. For use it you should compile your app as a system app. To do it you should follow next steps:
-
从Android源获取文件:
Getting files from android source:
根的,Android的源代码树/输出/主机//框架/ signapk.jar
根的,Android的源代码树/建设/目标/产品/安全/ platform.x509.pem
根的,Android的源代码树/建设/目标/产品/安全/ platform.pk8
使用获取文件签署您的应用程序:
sign your app using getting files:
命令 Java的罐子signapk.jar platform.x509.pem platform.pk8 YourApp-unsigned.apk YourApp-signed.apk。
Command "java -jar signapk.jar platform.x509.pem platform.pk8 YourApp-unsigned.apk" YourApp-signed.apk.
- 运行你的应用程序
- 使用亚行外壳PS,以确认您的应用程序正在运行的系统。
code触摸模拟(新线程是必要的模拟):
Code with touch simulating(new thread is necessary for simulation):
Thread thread = new Thread(){
@Override
public void run(){
Instrumentation m_Instrumentation = new Instrumentation();
m_Instrumentation.sendPointerSync(MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,posx, posy, 0));
m_Instrumentation.sendPointerSync(MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP,width*4/5,height, 0));
}
};
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp"
**android:sharedUserId="android.uid.system"**
android:versionCode="1"
android:versionName="1.0" >
使用资源:
- Programmatically在Android注射活动 - 第1部分
- How编译系统权限 Android应用程序
- Instruction对于编译这里
- Programmatically Injecting Events on Android – Part 1
- How to compile Android Application with system permissions
- Instruction for compile here