如何使用Android模擬觸摸事件,同時手動輸入X和Y座標?如何在Android中模擬觸摸事件?
回答
你應該給新的monkeyrunner一個去。也許這可以解決你的問題。你把鍵碼放在裏面進行測試,也許觸摸事件也是可能的。
請讓我知道如何安裝monkeyrunner。 adb不識別monkeyrunner – indira 2010-12-10 04:24:08
這與adb的ui excersiser猴子無關。你會在adt的版本9的工具目錄中找到monkeyrunner。 – keyboardsurfer 2010-12-10 11:38:05
同時查看我在答案中提供的鏈接。這導致Google提供的有關monkeyrunner的信息。 – keyboardsurfer 2010-12-10 12:26:51
如果我理解清楚,你想以編程方式做到這一點。然後,您可以使用方法View
,並用您需要的座標創建MotionEvent
。
MotionEvent僅通過觸摸屏幕生成。
這是一個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)
瓦倫丁金莎的方法工作,如果你擴展你的觀點,但如果您使用的是事件偵聽器,使用:
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?
當使用猴子腳本時,我注意到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
使用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
- 1. 從android內核模擬觸摸事件
- 2. 模擬iPhone中的觸摸事件
- 3. 在後臺線程中模擬Android中的觸摸事件
- 4. 用javascript模擬觸摸事件
- 5. BB10瀑布 - 模擬觸摸事件
- 6. 上YUI模擬觸摸事件3
- 7. 模擬觸摸事件在一定間隔的Android
- 8. 如何模擬Windows 7中的觸摸事件?
- 9. Android中的觸摸事件
- 10. 在Android模擬器中模擬多點觸摸
- 11. 如何延遲模擬的觸摸事件?
- 12. 如何禁用觸摸事件android
- 13. 在Windows 8中使用Python模擬觸摸事件
- 14. 關於在Android中的觸摸事件
- 15. 在Android中禁用WebView觸摸事件
- 16. Android Soundpool - 觸摸事件
- 17. Android TextView觸摸事件
- 18. Android的觸摸事件
- 19. 在iphone上模擬觸摸
- 20. Android藍牙觸摸板模擬
- 21. Android模擬器支持Windows觸摸屏
- 22. 觸摸屏上的Windows將通過觸摸手指來模擬MouseDown事件嗎?
- 23. 在EditText上模擬觸摸事件以顯示軟鍵盤
- 24. 在iOS上模擬系統寬觸摸事件
- 25. 有沒有辦法模擬Windows 8中的觸摸事件?
- 26. 是否有附加模塊或擴展模擬觸摸事件?
- 27. 觸摸事件
- 28. 如何在android中停止觸摸事件所需的時間?
- 29. 如何防止在Android中刷卡時發生觸摸事件?
- 30. 如何在觸摸事件android中添加移動圖像?
您在下面得到了一些可行的答案,請記住,它們只會在可以進行小修改的應用程序上工作。對於其他無法修改的應用程序,您需要一個有根的平臺來注入事件。 – 2010-12-09 08:20:24
是否可以讓應用程序每10秒觸摸x,y並將其最小化,但是在x,y中繼續觸摸? – 2014-11-22 16:40:57
檢查我的答案沒有根的要求。 :) – 2017-12-18 11:15:15