-2

When I try to run the following

process = Runtime.getRuntime().exec("input touchscreen swipe 100 1000 300 1000 1000"); //Normal swipe it works.

However, when I am using it as following, it is not working.
String[] inputs = {"adb", "shell", "input touchscreen swipe 500 1000 600 1000 1000"}; Process p = Runtime.getRuntime().exec(inputs); p.waitFor();

I've another command which I want to run, and for it to run, I have to use the second approach. Can someone tell me what is the reason or how can I make the second one run?

grad9
  • 39
  • 11

3 Answers3

0
Process p = Runtime.getRuntime().exec(inputs);

throws a couple of exceptions, including but not limited to IO, Null Pointer, Index Out of Bound.

Try surrounding that line with a try catch

Process p;
    try {
        p = Runtime.getRuntime().exec(ch);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  • I have specified exact snippet to keep the context clear. The catch blocks are already in place. Thanks.. – grad9 Jun 05 '18 at 07:02
0

You should paste what the error is, if it exists. (For example, in logcat.)

adb is the client, which may not exist in your Android device (since Android 5.0+). You are not expected to use it in your Android App command. For your sample, you can directly use input command without adb.

Also,

process = Runtime.getRuntime().exec("input touchscreen swipe 100 1000 300 1000 1000");

is equivalent to

String[] inputs = {"input", "touchscreen", "swipe", "100", "1000", "300", "1000", "1000"}; process = Runtime.getRuntime().exec(inputs);.

Note the dropped first two element in inputs from your question.

Geno Chen
  • 4,916
  • 6
  • 21
  • 39
  • There is no error. It just doesn't work.... As I mentioned in my question, the first one works smooth and fine. The second one doesn't. After changing to what you've suggested, it seemed to create just one event (may be a down) and then kept a lock, which resulted in an ANR. – grad9 Jun 05 '18 at 06:58
  • @grad9 As of my test (AIDE 3.2.161123, MIUI 8 7.3.23 on Android 6.0.1), I successfully run these two code in my answer, both with the expected phenomenon. I don't know if there are some errors on my phone (compile cache, or build cache, etc.) or in your code (coding error, etc.). – Geno Chen Jun 05 '18 at 07:09
  • I am using AIDE Android Studio 3.1.2, Build #AI-173.4720617 && Huawei CRR-L09, 5.1.1. Maybe you could find that the solution doesn't work on other devices/os versions etc. The example speaks of chained commands and chained args.. – grad9 Jun 05 '18 at 07:14
  • In the related questions, I found https://stackoverflow.com/questions/28337921/execute-adb-shell-input-swipe-command-from-apk, this question may possible duplicate. – Geno Chen Jun 05 '18 at 07:19
  • thanks for your help. p.waitFor(); had to be removed to get it working. – grad9 Jun 05 '18 at 07:22
0

As @Geno Chen mentioned, the parameters, need to go with double quotes. String[] inputs = {"input", "touchscreen", "swipe", "100", "1000", "300", "1000", "1000"}; p.waitFor(); must be removed to get this working.

grad9
  • 39
  • 11