Android 应用程序运行shell命令的实现
程序员文章站
2022-04-28 19:17:41
Android在应用程序中运行shell命令,可以通过如下工具类实现:
package com.example.test;
import java.io.Buffe...
Android在应用程序中运行shell命令,可以通过如下工具类实现:
package com.example.test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.util.Log; /** * 执行shell脚本工具类 * @author Mountain * */ public class CommandExecution { public static final String TAG = "CommandExecution"; 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; } }
在其他service中调用该类时,例如实现对eth0的IP地址配置,直接通过下述命令实现:
CommandExecution.execCommand("ifconfig eth0 192.168.1.2",true);
如果需要执行的命令包含变量,可以使用如下方式:
String ip = “192.168.1.2” String cmd = “ifconfig eth0 ”; cmd = cmd + ip; CommandExecution.execCommand(cmd,true);
在Android5.1中,上述命令并不能直接执行成功,会出现如下错误:
java.io.IOException Error running exec(). Command: [su] Working Directory: null Environment: null
修改frameworks/base/cmds/app_process/app_main.cpp文件
int main(int argc, char* const argv[]) { +/* if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { // Older kernels don't understand PR_SET_NO_NEW_PRIVS and return // EINVAL. Don't die on such kernels. return 12; } } +*/