加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 23:10:06 所属栏目:Linux 来源:网络整理
导读:我一直试图找出如何将触摸/键盘事件注入 Android设备一段时间(在您的应用程序内外). 我找到了一个没有root权限的应用程序: https://play.google.com/store/apps/details?id=com.vmlite.vncserver 有没有人知道他们是怎么做到的? 解决方法 如果你想在没有ro
我一直试图找出如何将触摸/键盘事件注入 Android设备一段时间(在您的应用程序内外).

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

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

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

解决方法

如果你想在没有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(),MotionEvent.ACTION_UP,0));
}
}

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读