AIDE教程-Android执行shell命令

Android中执行shell命令可通过如下工具类实现:

import android.util.*;
import java.io.*;

public class Command {
	public static final String TAG="Command";
	public final static String COMMAND_SU = "su";
	public final static String COMMAND_SH = "sh";
	public final static String COMMAND_EXIT = "exit\n";
	public final static String COMMAND_LINE_END = "\n";

	/**
	 * Command执行结果
	 * @author Mountain
	 *
	 */
	public static class CommandResult {
		public int result = -1;
		public String errorMsg;
		public String successMsg;
	}

	/**
	 * 执行命令—单条
	 * @param command
	 * @param isRoot
	 * @return
	 */
	public static CommandResult execCommand(String command, boolean isRoot) {
		String[] commands = {command};
		return execCommand(commands, isRoot);
	}

	/**
	 * 执行命令-多条
	 * @param commands
	 * @param isRoot
	 * @return
	 */
	public static CommandResult execCommand(String[] commands, boolean isRoot) {
		CommandResult commandResult = new CommandResult();
		if (commands == null || commands.length == 0) return commandResult;
		Process process = null;
		DataOutputStream os = null;
		BufferedReader successResult = null;
		BufferedReader errorResult = null;
		StringBuilder successMsg = null;
		StringBuilder errorMsg = null;
		try {
			process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
			os = new DataOutputStream(process.getOutputStream());
			for (String command : commands) {
				if (command != null) {
					os.write(command.getBytes());
					os.writeBytes(COMMAND_LINE_END);
					os.flush();
				}
			}
			os.writeBytes(COMMAND_EXIT);
			os.flush();
			commandResult.result = process.waitFor();
			//获取错误信息
			successMsg = new StringBuilder();
			errorMsg = new StringBuilder();
			successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
			errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
			String s;
			while ((s = successResult.readLine()) != null) successMsg.append(s);
			while ((s = errorResult.readLine()) != null) errorMsg.append(s);
			commandResult.successMsg = successMsg.toString();
			commandResult.errorMsg = errorMsg.toString();
			Log.i(TAG, commandResult.result + " | " + commandResult.successMsg
				  + " | " + commandResult.errorMsg);
		} catch (IOException e) {
			String errmsg = e.getMessage();
			if (errmsg != null) {
				Log.e(TAG, errmsg);
			} else {
				e.printStackTrace();
			}
		} catch (Exception e) {
			String errmsg = e.getMessage();
			if (errmsg != null) {
				Log.e(TAG, errmsg);
			} else {
				e.printStackTrace();
			}
		} finally {
			try {
				if (os != null) os.close();
				if (successResult != null) successResult.close();
				if (errorResult != null) errorResult.close();
			} catch (IOException e) {
				String errmsg = e.getMessage();
				if (errmsg != null) {
					Log.e(TAG, errmsg);
				} else {
					e.printStackTrace();
				}
			}
			if (process != null) process.destroy();
		}
		return commandResult;
	}
}

在其他Activity中调用该类时,列如复制AIDE教程网.txt文件到测试目录下,可通过如下命令实现:

//使用shell命令复制内存卡根目录下的AIDE教程网.txt到测试目录下
		Command.execCommand("cp -r /sdcard/AIDE教程网.txt /sdcard/测试/AIDE教程网.txt", true);

版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/954.html

梦雪

专注于AIDE教程分享

    留下你的评论

    *评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

    相关推荐