2010-12-09 196 views
85

如何使用Android模擬觸摸事件,同時手動輸入X和Y座標?如何在Android中模擬觸摸事件?

+1

您在下面得到了一些可行的答案,請記住,它們只會在可以進行小修改的應用程序上工作。對於其他無法修改的應用程序,您需要一個有根的平臺來注入事件。 – 2010-12-09 08:20:24

+0

是否可以讓應用程序每10秒觸摸x,y並將其最小化,但是在x,y中繼續觸摸? – 2014-11-22 16:40:57

+0

檢查我的答案沒有根的要求。 :) – 2017-12-18 11:15:15

回答

1

你應該給新的monkeyrunner一個去。也許這可以解決你的問題。你把鍵碼放在裏面進行測試,也許觸摸事件也是可能的。

+1

請讓我知道如何安裝monkeyrunner。 adb不識別monkeyrunner – indira 2010-12-10 04:24:08

+0

這與adb的ui excersiser猴子無關。你會在adt的版本9的工具目錄中找到monkeyrunner。 – keyboardsurfer 2010-12-10 11:38:05

+0

同時查看我在答案中提供的鏈接。這導致Google提供的有關monkeyrunner的信息。 – keyboardsurfer 2010-12-10 12:26:51

1

如果我理解清楚,你想以編程方式做到這一點。然後,您可以使用方法View,並用您需要的座標創建MotionEvent

-6

MotionEvent僅通過觸摸屏幕生成。

22

這是一個monkeyrunner腳本,用於將觸摸和拖動操作發送到應用程序。我一直在使用它來測試我的應用程序可以處理快速重複的滑動手勢。

# This is a monkeyrunner jython script that opens a connection to an Android 
# device and continually sends a stream of swipe and touch gestures. 
# 
# See http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html 
# 
# usage: monkeyrunner swipe_monkey.py 
# 

# Imports the monkeyrunner modules used by this program 
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice 

# Connects to the current device 
device = MonkeyRunner.waitForConnection() 

# A swipe left from (x1, y) to (x2, y) in 2 steps 
y = 400 
x1 = 100 
x2 = 300 
start = (x1, y) 
end = (x2, y) 
duration = 0.2 
steps = 2 
pause = 0.2 

for i in range(1, 250): 
    # Every so often inject a touch to spice things up! 
    if i % 9 == 0: 
     device.touch(x2, y, 'DOWN_AND_UP') 
     MonkeyRunner.sleep(pause) 
    # Swipe right 
    device.drag(start, end, duration, steps) 
    MonkeyRunner.sleep(pause) 
    # Swipe left 
    device.drag(end, start, duration, steps) 
    MonkeyRunner.sleep(pause) 
97

瓦倫丁金莎的方法工作,如果你擴展你的觀點,但如果您使用的是事件偵聽器,使用:

view.setOnTouchListener(new OnTouchListener() 
{ 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     Toast toast = Toast.makeText(
      getApplicationContext(), 
      "View touched", 
      Toast.LENGTH_LONG 
     ); 
     toast.show(); 

     return true; 
    } 
}); 


// Obtain MotionEvent object 
long downTime = SystemClock.uptimeMillis(); 
long eventTime = SystemClock.uptimeMillis() + 100; 
float x = 0.0f; 
float y = 0.0f; 
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState() 
int metaState = 0; 
MotionEvent motionEvent = MotionEvent.obtain(
    downTime, 
    eventTime, 
    MotionEvent.ACTION_UP, 
    x, 
    y, 
    metaState 
); 

// Dispatch touch event to view 
view.dispatchTouchEvent(motionEvent); 

更多關於獲得MotionEvent對象,這裏是一個優秀的回答:Android: How to create a MotionEvent?

0

當使用猴子腳本時,我注意到DispatchPress(KEYCODE_BACK) 沒有做任何事情,真的很爛。在很多情況下,這是由於Activity沒有使用Key事件。 此問題的解決方案是將猴腳本和 adb shell輸入命令按順序組合使用。

1使用猴子腳本給了一些偉大的時機 控制。等待一段時間的活動,並且是阻止adb呼叫的 。
2最後發送adb shell輸入keyevent 4將結束正在運行的APK。

EG

ADB殼猴-p com.my.application -v -v -v -f /sdcard/monkey_script.txt 1
ADB殼輸入的KeyEvent 4

17

使用ADB shell命令來模擬觸摸事件

adb shell input tap x y 

and also 

adb shell sendevent /dev/input/event0 3 0 5 
adb shell sendevent /dev/input/event0 3 1 29