对于linux驱动比较熟的人来说,这其实是很简单的一个实现。先简单了解下这个过程,方便以后拓展。 先说两种非ROOT的方法 第一种
第二种
第三种 系统驱动级的,需要ROOT权限,更直接彻底。只要有ROOT要取,不需要放到SYSTEM/APP下面也可以正常使用。
下面提供测试代码
MotionEvent evenDownt = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis() + 100, MotionEvent.ACTION_DOWN, 100, 400, 0); dispatchTouchEvent(evenDownt); MotionEvent eventUp = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis() + 100, MotionEvent.ACTION_UP, 100, 400, 0); dispatchTouchEvent(eventUp); evenDownt.recycle(); eventUp.recycle();这种方法仅支持当前的程序,其它的程序是没有任何效果的。
第二种
// 后台需要系统APP权限,如果你有ROOT权限,可以直接复制本APP到SYSTEM/APP/目录下 Instrumentation inst = new Instrumentation(); inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 200, 500, 0)); inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 200, 500, 0));程序界面显示可以实现点击,但是切换到后台的话会报错。解决方法:后台需要系统APP权限,如果你有ROOT权限,可以直接复制本APP到SYSTEM/APP/目录下,没有ROOT那就使用第一种方法吧。
第三种 系统驱动级的,需要ROOT权限,更直接彻底。只要有ROOT要取,不需要放到SYSTEM/APP下面也可以正常使用。
package com.lee.screenhelper; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import android.content.Context; public class Utils { private static Utils instance; private Utils(Context context) { super(); // TODO Auto-generated constructor stub try { Runtime.getRuntime().exec("su"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Utils getInstance(Context context) { if (instance == null) instance = new Utils(context); return instance; } public void screenshot() { exec("chmod 777 /dev/graphics/fb0 \n cat /dev/graphics/fb0 > /mnt/sdcard/fb0", null); } public void sendClick(int x, int y) { String[] orders = { "sendevent /dev/input/event4 0 0 0", "sendevent /dev/input/event4 1 330 1", "sendevent /dev/input/event4 3 53 " + x, "sendevent /dev/input/event4 3 54 " + y, "sendevent /dev/input/event4 0 0 0", "sendevent /dev/input/event4 1 330 0", "sendevent /dev/input/event4 0 0 0", "sendevent /dev/input/event4 0 0 0" }; exec("su", orders); } public void sendHome() { String[] orders = { "sendevent /dev/input/event1 0 0 0", "sendevent /dev/input/event1 1 102 1", "sendevent /dev/input/event1 0 0 0", "sendevent /dev/input/event1 1 102 0", "sendevent /dev/input/event1 0 0 0", "sendevent /dev/input/event1 0 0 0" }; exec("su", orders); } private void exec(String cmd, String[] orders) { try { Process process = Runtime.getRuntime().exec(cmd); DataOutputStream dataOut = new DataOutputStream( process.getOutputStream()); if (orders != null) { for (String order : orders) dataOut.writeBytes(order + ";"); } dataOut.flush(); dataOut.close(); process.waitFor(); InputStream in = process.getInputStream(); BufferedReader bufferReader = new BufferedReader( new InputStreamReader(in)); BufferedReader err = new BufferedReader(new InputStreamReader( process.getErrorStream())); String line = null; while ((line = err.readLine()) != null) System.out.println("1.>>>" + line); while ((line = bufferReader.readLine()) != null) System.out.println("2.>>>" + line); } catch (Exception e) { e.printStackTrace(); } finally { } } }
下面提供测试代码
package com.androidtouch; import android.app.Activity; import android.app.Instrumentation; import android.os.Bundle; import android.os.Handler; import android.os.SystemClock; import android.view.MotionEvent; import android.view.Window; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub setMouseClick(); } }, 5000); // test2(); } public void test2() { new Thread() { @Override public void run() { // TODO Auto-generated method stub super.run(); try { Thread.sleep(5000); setMouseClick2(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } // 模拟屏幕点击事件 - 只在 Activity 中有用 也可后台使用(本APP,其它无效) public void setMouseClick() { MotionEvent evenDownt = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis() + 100, MotionEvent.ACTION_DOWN, 100, 400, 0); dispatchTouchEvent(evenDownt); MotionEvent eventUp = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis() + 100, MotionEvent.ACTION_UP, 100, 400, 0); dispatchTouchEvent(eventUp); evenDownt.recycle(); eventUp.recycle(); } public void setMouseClick2() { // 后台需要系统APP权限,如果你有ROOT权限,可以直接复制本APP到SYSTEM/APP/目录下 Instrumentation inst = new Instrumentation(); inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 200, 500, 0)); inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 200, 500, 0)); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() == MotionEvent.ACTION_UP) Toast.makeText(this, "x:" + event.getX() + " y:" + event.getY(), Toast.LENGTH_SHORT).show(); return super.onTouchEvent(event); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.exit(0); } }
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2300
- 用户1336
- 访客10859210
每日一句
True success inspires others to act.
真正的成功是激励他人行动。
真正的成功是激励他人行动。
语法错误: 意外的令牌“标识符”
全面理解Gradle - 定义Task
Motrix全能下载工具 (支持 BT / 磁力链 / 百度网盘)
谷歌Pixel正在开始起飞?
获取ElementUI Table排序后的数据
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is
亲测!虚拟机VirtualBox安装MAC OS 10.12图文教程
华为手机app闪退重启界面清空log日志问题
android ndk开发之asm/page.h: not found
手机屏幕碎了怎么备份操作?
免ROOT实现模拟点击任意位置
新手必看修改DSDT教程
thinkpad t470p装黑苹果系统10.13.2
新会员