Programing

Android에서 터치 이벤트를 시뮬레이션하는 방법은 무엇입니까?

lottogame 2020. 8. 25. 19:19
반응형

Android에서 터치 이벤트를 시뮬레이션하는 방법은 무엇입니까?


X 및 Y 좌표를 수동으로 제공하면서 Android로 터치 이벤트를 시뮬레이션하는 방법은 무엇입니까?


Valentin Rocher의 방법은 뷰를 확장 한 경우 작동하지만 이벤트 리스너를 사용하는 경우 다음을 사용하십시오.

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 : 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)

adb 셸 명령을 사용하여 터치 이벤트 시뮬레이션

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 

내가 명확하게 이해한다면 프로그래밍 방식으로 수행하고 싶습니다. 그런 다음의 onTouchEvent 메서드를 사용하고 필요한 좌표로를 View만들 MotionEvent수 있습니다.


You should give the new monkeyrunner a go. Maybe this can solve your problems. You put keycodes in it for testing, maybe touch events are also possible.


When using Monkey Script I noticed that DispatchPress(KEYCODE_BACK) is doing nothing which really suck. In many cases this is due to the fact that the Activity doesn't consume the Key event. The solution to this problem is to use a mix of monkey script and adb shell input command in a sequence.

1 Using monkey script gave some great timing control. Wait a certain amount of second for the activity and is a blocking adb call.
2 Finally sending adb shell input keyevent 4 will end the running APK.

EG

adb shell monkey -p com.my.application -v -v -v -f /sdcard/monkey_script.txt 1
adb shell input keyevent 4


MotionEvent is generated only by touching the screen.

참고URL : https://stackoverflow.com/questions/4396059/how-to-simulate-a-touch-event-in-android

반응형