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

查看:189
本文介绍了如何模拟触摸,从后台服务与的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):

<一个href="http://stackoverflow.com/questions/7011626/unable-to-execute-sendevent-shell-command-through-the-android-$c$c">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的Andr​​oid 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()从a​​ndroid.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:

  1. 从Android源获取文件:

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

        

使用资源:

  • <一个href="http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/#.UUu1jzdj8fX">Programmatically在Android注射活动 - 第1部分
  • <一个href="http://stackoverflow.com/questions/3598662/how-to-compile-android-application-with-system-permissions">How编译系统权限
  • Android应用程序
  • <一个href="https://groups.google.com/group/Android-DevPhone-Updating/tree/browse_frm/month/2010-04?_done=/group/Android-DevPhone-Updating/browse_frm/month/2010-04?&&pli=1">Instruction对于编译这里
  • Programmatically Injecting Events on Android – Part 1
  • How to compile Android Application with system permissions
  • Instruction for compile here

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

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