/** * shell . * * @param command shell . */ public void runCommand(final String command) { // , new Thread(new Runnable() { public void run() { OutputStream out = null; InputStream in = null; try { // Process child = Runtime.getRuntime().exec(command); // out = child.getOutputStream(); in = child.getInputStream(); // - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); String line; String result = ""; while ((line = bufferedReader.readLine()) != null) result += line; // , handleBashCommandsResult(result); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } } }).start(); }
Process child = Runtime.getRuntime().exec(command);
Process child = Runtime.getRuntime().exec(new String[] { "su", "-c", "system/bin/sh" }); DataOutputStream stdin = new DataOutputStream(child.getOutputStream()); // stdin.writeBytes(command);
/** * * * @param i * @return */ public static String doSleep(int i) { return "adb shell 'sleep " + i + "' ;"; } /** * * * @param x1 * @param y1 * @param x2 * @param y2 * @return */ public static String doSwipe(int x1, int y1, int x2, int y2) { return "adb shell input swipe " + x1 + " " + y1 + " " + x2 + " " + y2 + " ;"; } /** * * * @param x * @param y * @return */ public static String doTap(int x, int y) { return "adb shell input tap " + x + " " + y + " ;"; } /** * * * @param text * @return */ public static String doInputText(String text) { return "adb shell input text " + text + " ;"; } /** * * * @param keycode * @return */ public static String doInputKeyevent(int keycode) { return "adb shell input keyevent " + keycode + " ;"; } /** * ( ) * * @param message * @return */ public static String echo(String message) { return "echo '" + message + "' ;"; }
/** * * * @return */ public static String sampleScript(){ String command = ""; command = command .concat(doSwipe(100, 200, 100, 500)) .concat(doSleep(1)) .concat(doTap(100, 150)) .concat(doSleep(1)) .concat(doInputText(" ")) .concat(doSleep(1)) .concat(doInputKeyevent(KeyEvent.KEYCODE_ENTER)) .concat(doSleep(1)) .concat(echo("SCRIPT_FINISHED")); return command; }
runCommand(sampleScript());
/** * , ( echo) * * @param result . */ private void handleBashCommandsResult(String result) { if (result.contains("SCRIPT_FINISHED")) { // } else if (.....){ // - } else { // } }
Source: https://habr.com/ru/post/264135/
All Articles