2

I want to implement a click on my app programmatically. For doing that I thought of implementing the adb command for touch. The command is: adb shell input tap x y This command works on my adb shell, but i cant figure out a way of implementing it programmatically. I tried the below piece of code:

private void runShellCommand(String command) throws Exception {
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();
        }

But I am getting this error in the debugger:

java.io.IOException: Cannot run program "adb": error=13, Permission denied

Could someone please help me with this!

PS: Even if you know other methods of programmatically touching the screen with the help of providing x and y coordinates please do help!

2
  • Are you trying to open the app automatically? Commented Aug 2, 2020 at 7:48
  • @Pronoy999 no. I have made an app. In that app I would give an input of x and y coordinates. After that I will click on a button. After clicking on the button, I would want the app to automatically click on co-ordinates which I have entered. Commented Aug 2, 2020 at 7:52

2 Answers 2

1

I am assuming that you want to touch something inside your app.

Capture an ACTION_DOWN MotionEvent (through the debugger from a touch action) and note its properties (down time, event time, and the meta state). This would only need to be done once to figure out what sort of values you should use to simulate a typical touch event.

In your test program, create a new MotionEvent with MotionEvent.obtain()

MotionEvent newTouch = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN,
    x, y, metaState);

Dispatch the event on your view:

view.dispatchTouchEvent(newTouch);

You can try this. I have been using this for my own android test app.

Sign up to request clarification or add additional context in comments.

4 Comments

oh... could you please tell me how could I capture the ACTION_DOWN MotionEvent?
Okay so I got all the required things. I passed in a view as a button it worked. But when I pass in the view as a RelativeLayout (the parent of layout file) the touch doesn't work. Does this not work on the primary layout (first/parent layout) of the screen?
I never tried with a relative layout. Can you try it with a Constraint Layout and check?
Do let me know if you have some sort of solution. In the meantime I am also hunting...
0
  1. Each app in Android runs under separate user, and these users have no right to execute adb. Thus, you get "Permission Denied".
  2. Actually, you don't need adb. Your command requests adb to invoke a shell to run input. So, you can execute input x y directly.

2 Comments

i tried running it as you mentioned in point 2, but that just makes the error go away. There is no touch happening...
try to use the way Pronoy999 suggested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.