如何使用 sendevent 或其他方式模拟来自后台服务的触摸? [英] How to simulate touch from background service with sendevent or other way?

查看:33
本文介绍了如何使用 sendevent 或其他方式模拟来自后台服务的触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从后台应用程序(或服务)模拟触摸或运行 sh 脚本(模拟触摸)?

Is it possible to simulate touch from the background application (or service) or to run sh script (that simulate touch)?

需要在没有 USB 或其他连接到 PC 的情况下测试 android 系统,这就是为什么我不能(或不知道如何)使用 Monkey 或其他自动测试工具.

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):

无法通过android执行sendevent shell命令代码(创建触摸模拟).在系统分区上写入文件(以root权限运行命令)

Unable to execute sendevent shell command through the android code (create touch simulation). 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
",
                        "sendevent /dev/input/event6 3 53 499
",
                        "sendevent /dev/input/event6 3 54 680
",
                        "sendevent /dev/input/event6 3 58 40
",
                        "sendevent /dev/input/event6 3 48 3
",
                        "sendevent /dev/input/event6 3 57 0
",
                        "sendevent /dev/input/event6 0 2 0
",
                        "sendevent /dev/input/event6 0 0 0
",
                        "sendevent /dev/input/event6 0 2 0
",
                        "sendevent /dev/input/event6 0 0 0
",
                        "sendevent /dev/input/event0 3 0 2
",
                        "sendevent /dev/input/event0 0 0 0
"};

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
");
    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?

如果有其他方法可以使用 android ndk 或 C 上的 daemon,请告诉我.

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.

对于模拟触摸,我使用了 android.app.Instrumentation 中的 sendPointerSync(),它仅适用于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:

  1. 从安卓源获取文件:

  1. Getting files from android source:

root-of-android-source-tree/out/host//framework/signapk.jar

root-of-android-source-tree/build/target/product/security/platform.x509.pem

root-of-android-source-tree/build/target/product/security/platform.pk8

使用获取文件为您的应用签名:

sign your app using getting files:

命令java -jar 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.

  • 运行您的应用
  • 使用adb shell ps"确认您的应用正在作为系统运行.

带触摸模拟的代码(模拟需要新线程):

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" >

<强>

使用资源:

这篇关于如何使用 sendevent 或其他方式模拟来自后台服务的触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆