Stack Overflow archive
3 scoreaccepted

execute adb shell input swipe command from apk

score
3
question views
5.7K
license
CC BY-SA 3.0

You can only execute /system/bin/input as the root or shell user; this will not work in an app. The command should not start with "adb shell" when running from the app.

To run the command as root:

bash
Process su = null; 
try { 
    su = Runtime.getRuntime().exec("su");
    su.getOutputStream().write("input swipe 250 300 -800 300\n".getBytes());
    su.getOutputStream().write("exit\n".getBytes());
    su.waitFor(); 
} catch (Exception e) {
    e.printStackTrace();
} finally { 
    if (su != null) { 
        su.destroy(); 
    } 
}

You should also check out third party libraries for handling su commands: https://android-arsenal.com/details/1/451

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.