使用Instrumentation模拟点击屏幕

1.使用前需要申明权限

<uses-permission android:name="android.permission.INJECT_EVENTS" />

2.使用Instrumentation模拟点击有一定限制,只能点击自己的APP,或者UID相同的APP,如果有root权限或者系统签名则可以点击所有APP

import android.app.Instrumentation;
    
public static void tap(int x,int y){
        try{
            getInst().sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                    SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, 0));    //x,y 即是事件的坐标
            getInst().sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                    SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0));
        }catch (Exception e){
            ToastUtils.showShort("点击出错");
        }
    }
posted @ 2018-09-19 23:28  土豆吞噬者  阅读(353)  评论(0编辑  收藏  举报