Touch, swipe or drag events emulating on Android n

2019-05-11 15:32发布

问题:

After reading posts I figured out how to emulate a touch event :

adb shell input tap 100 100

I've installed MagicMarker in order to see if anything is drawn, nothing appears.

I've also tried with monkeyrunner/androidViewClient Touch functions :

device.touch(100 , 100, 'DOWN_AND_UP');

My whole code for AndroidViewClient :

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import getopt, sys
import os

# Add android to path becayuse it seems to not appear on windows

sys.path.append("D:/Dev/adt-bundle-windows-x86_64-20131030/sdk/tools")
sys.path.append("D:/Dev/adt-bundle-windows-x86_64-20131030/sdk/tools/lib")
sys.path.append("D:/Dev/adt-bundle-windows-x86_64-20131030/sdk/platform-tools")

# PyDev sets PYTHONPATH, use it
try:
    for p in os.environ['PYTHONPATH'].split(':'):
        if not p in sys.path:
            sys.path.append(p)
except:
    pass

try:
    sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

from com.dtmilano.android.viewclient import ViewClient, ViewNotFoundException
device, serialno = ViewClient.connectToDeviceOrExit()   
vc = ViewClient(device, serialno)
device.touch(100,100,"DOWN_AND_UP")

I successfully used device.press("KEYCODE_MENU", "DOWN_AND_UP") or device.takeSnapshot(), I don't understand at all why touch events are not received by my handset.

By the way, I am using real devices (a GS3 and GS4 both in 4.3)

Do not hesitate to ask further information.

回答1:

I've tested MagicMarker using this simple AndroidViewClient script. Notice that some fixes to adbclient.drag() were introduced in version 5.1.1 so be sure you have the latest version.

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2014  Diego Torres Milano
'''

__requires__ = ["androidviewclient >= 5.1.1"]
import pkg_resources
from com.dtmilano.android.adb.adbclient import AdbClient

AdbClient(serialno='.*').drag((100, 100), (400, 400), 1000)

This produces:

Also notice than in your script device is the AdbClient instance.

If you check how drag() is implemented in AdbClient, you'll see that is using input swipe with parameters according to the corresponding API level:

def drag(self, (x0, y0), (x1, y1), duration, steps=1):
'''
Sends drag event (actually it's using C{input swipe} command.

@param (x0, y0): starting point
@param (x1, y1): ending point
@param duration: duration of the event in ms
@param steps: number of steps (currently ignored by @{input swipe}
'''

version = int(self.getProperty('ro.build.version.sdk'))
if version <= 15:
    raise RuntimeError('drag: API <= 15 not supported (version=%d)' % version)
elif version <= 17:
    self.shell('input swipe %d %d %d %d' % (x0, y0, x1, y1))
else:
    self.shell('input swipe %d %d %d %d %d' % (x0, y0, x1, y1, duration))