在没有root的情况下在Android中注入事件

时间:2014-01-29 15:18:27

标签: android events inject

我一直试图弄清楚如何将触摸/键盘事件注入Android设备一段时间(在您的应用程序内外)。

我找到了一个没有root权限的应用程序:

https://play.google.com/store/apps/details?id=com.vmlite.vncserver

有没有人知道他们是怎么做到的?

2 个答案:

答案 0 :(得分:3)

如果你想在没有root的Android应用程序上注入触摸事件:

你可以使用Instrumentation类, https://developer.android.com/reference/android/app/Instrumentation.html

import android.app.Instrumentation;

public class MainActivity extends Activity {
Instrumentation m_Instrumentation;
  public void onCreate(Bundle savedInstanceState) {
    m_Instrumentation = new Instrumentation();

    int x=0; //your x coord in screen. 
    int y=0; // your y coord in screen. 
    m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                    SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,x, y,0));
            m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                    SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,x, y,0));
}
}

此方法获取ACTION_DOWN和ACTION_UP事件,该事件在当前布局上注入事件。

注意:如果您的坐标(x,y)的注入超出屏幕大小,则应用程序将崩溃。 此方法注入仅适用于app内部,如果要注入触摸事件,则需要root设备并通过adb命令注入事件。

答案 1 :(得分:2)

  

对于非root设备,每次关闭和打开设备后,您都必须使用USB线将设备连接到Windows PC或Mac,然后运行免费的桌面程序,VMLite Android App Controller,在您的设备上启动服务器。

我很确定在此步骤中它会更改/dev/input/event..个文件的权限,并通过编写注释来执行注入(回避Android VM)。此博客文章详细介绍了此技术:Part1Part2