0

I need to simulate tap event on android using command. I use this code but it is not working:

public Boolean execCommands(String... command) {
    try {
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());

        for(int i = 0; i < command.length; i++) {
            os.writeBytes(command[i] + "\n");
            os.flush();
        }
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (IOException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    }
    return true; 
}

where command is:

String[] commands = {
        "/system/bin/input tap 250 450",
        "/system/bin/input tap 250 450"
};
5
  • Do you have the right permissions in the manifest? Commented Aug 25, 2014 at 11:25
  • In my manifest I have this permission: <uses-permission android:name="android.permission.ACCESS_SUPERUSER" /> Phone is properly rooted and acces in superuser is granted Commented Aug 25, 2014 at 11:28
  • Do you get an exception? Commented Aug 25, 2014 at 11:32
  • 1
    No nothing, im trying to click on button. 250 450 are they coordinates. And on click listener I have log but onClick event is not trigered. Commented Aug 25, 2014 at 11:34
  • If I use it from console it works Commented Aug 25, 2014 at 12:00

1 Answer 1

1

Try this:

List<String> envList = new ArrayList<String>();
Map<String, String> envMap = System.getenv();
for (String envName : envMap.keySet()) {
    envList.add(envName + "=" + env.get(envName));
}
String[] environment = (String[]) envList.toArray(new String[0]);
String command = "/system/bin/input tap 250 450\n" +
    "/system/bin/input tap 250 450";
try {
    Runtime.getRuntime().exec(
        new String[] { "su", "-c", command }, 
        environment);
}catch(IOException e) {
    Log.e(tag, Log.getStackTraceString(e));
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tested and it only working for touch event. Not come to on click event.

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.