我一直试图弄清楚如何将触摸/键盘事件注入Android设备一段时间(在您的应用程序内外)。
我找到了一个没有root权限的应用程序:
https://play.google.com/store/apps/details?id=com.vmlite.vncserver
有没有人知道他们是怎么做到的?
答案 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)