public void execute() { if (isRunning()) { return; } mWorker = ThreadHandler.getInstance() .createThread( new Runnable() { @Override public void run() { try { presetting(); String cmd = getCommand(); Log.i(Const.LOG_TAG, "cmd: " + cmd); if (cmd == null || cmd.isEmpty()) { return; } // execute Process process = Runtime.getRuntime().exec(cmd); // Process process = Runtime.getRuntime().exec("su"); // DataOutputStream os = new DataOutputStream(process.getOutputStream()); // os.writeBytes(cmd + "\n"); // os.flush(); // os.writeBytes("exit\n"); mInput = ThreadHandler.getInstance() .createThread(new StreamGobbler(process.getInputStream(), mInputCB)); mError = ThreadHandler.getInstance() .createThread(new StreamGobbler(process.getErrorStream(), mErrorCB)); mInput.start(); mError.start(); notifyListener(RUNNNING); process.waitFor(); notifyListener(FINISH); } catch (Exception e) { notifyListener(STOP_EXCEPTION); } } }); mWorker.start(); }
public void stop() { Thread thread = ThreadHandler.getInstance() .createThread( new Runnable() { public void run() { if (mWorker != null) { ArrayList<Integer> pLisId = getProcessId("uiautomator"); if (pLisId != null && pLisId.size() > 0) { for (int i = 0; i < pLisId.size(); i++) { // Kill process int pId = pLisId.get(i).intValue(); try { Log.i(Const.LOG_TAG, "kill process:" + pId); Runtime.getRuntime() .exec( new String[] { "/system/xbin/su", "0", "/system/bin/sh", "-c", "kill -9 " + pId }); } catch (Exception e) { Log.e(Const.LOG_TAG, e.getMessage(), e); } } } ThreadHandler.getInstance().interruptThread(mWorker); mWorker = null; } if (mInput != null) { ThreadHandler.getInstance().interruptThread(mInput); mInput = null; } if (mError != null) { ThreadHandler.getInstance().interruptThread(mError); mError = null; } } }); thread.start(); }