Android模拟键盘鼠标事件

 通过Socket + Instrumentation实现模拟键盘鼠标事件主要通过以下三个部分组成;

  Socket编程:实现PC和Emulator通讯,并进行循环监听;

  Service服务:将Socket的监听程序放在Service中,从而达到后台运行的目的。这里要说明的是启动服务有两种方式,bindService和startService,两者的区别是,前者会使启动的Service随着启动Service的Activity的消亡而消亡,而startService则不会这样,除非显式调用stopService,否则一直会在后台运行因为Service需要通过一个Activity来进行启动,所以采用startService更适合当前的情形;

  Instrumentation发送键盘鼠标事件:Instrumentation提供了丰富的以send开头的函数接口来实现模拟键盘鼠标,如下所述:
  sendCharacterSync(int keyCode)            //用于发送指定KeyCode的按键
  sendKeyDownUpSync(int key)                //用于发送指定KeyCode的按键
  sendPointerSync(MotionEvent event)     //用于模拟Touch
  sendStringSync(String text)                   //用于发送字符串

  注意:以上函数必须通过Message的形式抛到Message队列中。如果直接进行调用加会导致程序崩溃。
  对于Socket编程和Service网上有很多成功的范例,此文不再累述,下面着重介绍一下发送键盘鼠标模拟事件的代码:

  发送键盘KeyCode:
  步骤1. 声明类handler变量
  private static Handler handler;

  步骤2. 循环处理Message

  java代码:

  //在Activity的onCreate方法中对下列函数进行调用
  private void createMessageHandleThread(){
  //need start a thread to raise looper, otherwise it will be blocked
  Thread t = new Thread() {
  public void run() {
  Log.i( TAG,"Creating handler ..." );
  Looper.prepare();
  handler = new Handler(){
  public void handleMessage(Message msg) {
  //process incoming messages here
  }
  };
  Looper.loop();
  Log.i( TAG, "Looper thread ends" );
  }
  };
  t.start();

  步骤3. 在接收到Socket中的传递信息后抛出Message

  java代码:

  handler.post( new Runnable() {
  public void run() {
  Instrumentation inst=new Instrumentation();
  inst.sendKeyDownUpSync(keyCode);
  }
  } );

  Touch指定坐标,如下例子即
  java代码:

  touch point(240,400)
  Instrumentation inst=new Instrumentation();
  inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN, 240, 400, 0));
  inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_UP, 240, 400, 0));

Android 中,可以使用 `Instrumentation` 类来模拟用户操作,包括点击、滑动和键盘事件等。下面是一些示例代码: 1. 模拟点击事件 ``` // 获取当前 Activity 的 Instrumentation 对象 Instrumentation instrumentation = new Instrumentation(); // 模拟点击屏幕上的 (x, y) 坐标处 instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, 0)); // 延迟 100 毫秒 SystemClock.sleep(100); // 模拟松开手指 instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0)); ``` 2. 模拟滑动事件 ``` // 获取当前 Activity 的 Instrumentation 对象 Instrumentation instrumentation = new Instrumentation(); // 模拟滑动事件,从 (x1, y1) 滑动到 (x2, y2),持续时间为 duration 毫秒 long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis(); MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x1, y1, 0); instrumentation.sendPointerSync(event); eventTime += duration / 2; event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, (x1 + x2) / 2, (y1 + y2) / 2, 0); instrumentation.sendPointerSync(event); eventTime += duration / 2; event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x2, y2, 0); instrumentation.sendPointerSync(event); ``` 3. 模拟键盘事件 ``` // 获取当前 Activity 的 Instrumentation 对象 Instrumentation instrumentation = new Instrumentation(); // 模拟按下键盘上的某个键 instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_A); // 延迟 100 毫秒 SystemClock.sleep(100); // 模拟释放键盘上的某个键 instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_A); ``` 需要注意的是,模拟用户操作需要在主线程之外进行,否则会导致程序异常。可以使用 Handler 或 AsyncTask 等方式来实现在子线程中模拟用户操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值