我需要使用命令在android上模拟tap事件。我使用此代码但它无法正常工作:
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;
}
其中命令是:
String[] commands = {
"/system/bin/input tap 250 450",
"/system/bin/input tap 250 450"
};
答案 0 :(得分:1)
试试这个:
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));
}