Android模拟点击的四种方式

               

导论

在Android中模拟一个点击事件有三种方式是通过模拟MotionEvent来实现;一种是通过ADB来实现;一种是通过Instrumentation测试框架来实现


第一种:模拟MotionEvent

通用方法如下:

private void setSimulateClick(View view, float x, float y) {  long downTime = SystemClock.uptimeMillis();  final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,    MotionEvent.ACTION_DOWN, x, y, 0);  downTime += 1000;  final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,    MotionEvent.ACTION_UP, x, y, 0);  view.onTouchEvent(downEvent);  view.onTouchEvent(upEvent);  downEvent.recycle();  upEvent.recycle(); }

Demo:

package com.xys.simulateevent;import android.app.Activity;import android.os.Bundle;import android.os.SystemClock;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity { Button button; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  button = (Button) findViewById(R.id.button1); } public void clickeMe(View view) {  Toast.makeText(this, "clicked", Toast.LENGTH_LONG).show(); } public void simulate(View view) {  setSimulateClick(button, 160, 100); } private void setSimulateClick(View view, float x, float y) {  long downTime = SystemClock.uptimeMillis();  final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,    MotionEvent.ACTION_DOWN, x, y, 0);  downTime += 1000;  final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,    MotionEvent.ACTION_UP, x, y, 0);  view.onTouchEvent(downEvent);  view.onTouchEvent(upEvent);  downEvent.recycle();  upEvent.recycle(); }}

第二种:ADB控制

ADB中通过input来实现,用于输入 touch,key等事件
The sources are:       trackball      joystick      touchnavigation      mouse      keyboard      gamepad      touchpad      dpad      stylus      touchscreenThe commands and default sources are:      text <string> (Default: touchscreen)      keyevent [--longpress] <key code number or name> ... (Default: keyboard)      tap <x> <y> (Default: touchscreen)      swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)      press (Default: trackball)      roll <dx> <dy> (Default: trackball)

Demo 输入按键enter key:
adb shell input keyevent 66

Demo 输入输入滑动操作:
adb shell input touchscreen swipe 18 665 18 350

第三种:Instrumentation

Instrumentation是Android的测试框架,通过他,可以模拟很多Activity操作

        //KeyEvent.KEYCODE_MENU //KeyEvent.KEYCODE_BACK  public static void sendKeyEvent(final int KeyCode) {        new Thread() {     //不可在主线程中调用             public void run() {                 try {                     Instrumentation inst = new Instrumentation();                     inst.sendKeyDownUpSync(KeyCode);                } catch (Exception e) {                     e.printStackTrace();                 }              }             }.start();  }


第四种:通过使用sendevent

adb提供了控制手机的另一种实现:   sendevent [device] [type] [code] [value]
eg:229就是menu的type值
sendevent /dev/input/event0 1 229 1

sendevent /dev/input/event0 1 229 0

这两句需要一起执行,因为一个点击事件包含down和up,不然不能执行操作

           

你可能感兴趣的:(Android模拟点击的四种方式)