Android7模拟点击,android模拟点击方法全面解析

对于linux驱动比较熟的人来说,这其实是很简单的一个实现。先简单了解下这个过程,方便以后拓展。先说两种非ROOT的方法第一种 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);

}

}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值