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?
    – hoomi
    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?
    – hoomi
    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));
}
1
  • I tested and it only working for touch event. Not come to on click event.
    – YeeKhin
    Commented Jun 22, 2017 at 7:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.