1

I am trying to execute swipe command from my Apk using

process = Runtime.getRuntime().exec("adb shell input swipe 250 300 -800 300");

but nothing happens and no error occurs during runtime.

Do i have to add anything in manifest to get it to work?

K Guru
  • 1,292
  • 2
  • 17
  • 36
Anonymous
  • 53
  • 1
  • 7

2 Answers2

3

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:

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

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • Thanks a lot..works fine was working on it since last two weeks :) – Anonymous Feb 06 '15 at 09:48
  • I don't get an exception but it doesn't do anything for me. Tried it from adb and saw the swipe produced, just can't get it to work from withing the app. How can I check what is going wrong? Do I need the app the be signed as system app? – Stephan GM Jun 23 '16 at 21:06
1

You need to put INJECT_EVENTS permission in your manifest:



uses-permission android:name="android.permission.INJECT_EVENTS"

Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54