Search code examples
androidadb

Long press Android inside Application


I'm trying to simulate long taps on an Android device using ADB.

I read this guide: http://ktnr74.blogspot.it/2013/06/emulating-touchscreen-interaction-with.html

And I knew that it works on the UI of Android but it doesn't work inside an application.

In particular, I'm trying to simulate long-taps on a text field of an Application.


Solution

  • Edit: First of all you will need root for this. To ask for root in runtime:

    public static Process requestPermission() {
        try {
            return Runtime.getRuntime().exec("su");
        } catch (IOException e) {
            LOGGER.error("The phone needs root: ", e.getMessage());
            return null;
        }
    }
    

    Then:

    Process process = SystemUtils.requestPermission();
    DataOutputStream os = new DataOutputStream(process.getOutputStream());
    
    cmd = "/system/bin/input swipe 100 1650 600 1650\n"; //Example
    
    os.writeBytes(cmd);
    os.writeBytes("exit\n");
    os.flush();
    os.close();
    process.waitFor();
    

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

    You can find the "adb shell keyevent" commands here