3.1.Runtime
好,最后重点说一下Runtime,他在java.lang包下面,
/** * Executes the specified command and its arguments in a separate native * process. The new process inherits the environment of the caller. Calling * this method is equivalent to calling {@code exec(progArray, null, null)}. * * @param progArray * the array containing the program to execute as well as any * arguments to the program. * @return the new {@code Process} object that represents the native * process. * @throws IOException * if the requested program can not be executed. */ public Process exec(String[] progArray) throws java.io.IOException { return exec(progArray, null, null); } /** * Executes the specified command and its arguments in a separate native * process. The new process uses the environment provided in {@code envp}. * Calling this method is equivalent to calling * {@code exec(progArray, envp, null)}. * * @param progArray * the array containing the program to execute as well as any * arguments to the program. * @param envp * the array containing the environment to start the new process * in. * @return the new {@code Process} object that represents the native * process. * @throws IOException * if the requested program can not be executed. */ public Process exec(String[] progArray, String[] envp) throws java.io.IOException { return exec(progArray, envp, null); } /** * Executes the specified command and its arguments in a separate native * process. The new process uses the environment provided in {@code envp} * and the working directory specified by {@code directory}. * * @param progArray * the array containing the program to execute as well as any * arguments to the program. * @param envp * the array containing the environment to start the new process * in. * @param directory * the directory in which to execute the program. If {@code null}, * execute if in the same directory as the parent process. * @return the new {@code Process} object that represents the native * process. * @throws IOException * if the requested program can not be executed. */ public Process exec(String[] progArray, String[] envp, File directory) throws IOException { // ProcessManager is responsible for all argument checking. return ProcessManager.getInstance().exec(progArray, envp, directory, false); } /** * Executes the specified program in a separate native process. The new * process inherits the environment of the caller. Calling this method is * equivalent to calling {@code exec(prog, null, null)}. * * @param prog * the name of the program to execute. * @return the new {@code Process} object that represents the native * process. * @throws IOException * if the requested program can not be executed. */ public Process exec(String prog) throws java.io.IOException { return exec(prog, null, null); } /** * Executes the specified program in a separate native process. The new * process uses the environment provided in {@code envp}. Calling this * method is equivalent to calling {@code exec(prog, envp, null)}. * * @param prog * the name of the program to execute. * @param envp * the array containing the environment to start the new process * in. * @return the new {@code Process} object that represents the native * process. * @throws IOException * if the requested program can not be executed. */ public Process exec(String prog, String[] envp) throws java.io.IOException { return exec(prog, envp, null); } /** * Executes the specified program in a separate native process. The new * process uses the environment provided in {@code envp} and the working * directory specified by {@code directory}. * * @param prog * the name of the program to execute. * @param envp * the array containing the environment to start the new process * in. * @param directory * the directory in which to execute the program. If {@code null}, * execute if in the same directory as the parent process. * @return the new {@code Process} object that represents the native * process. * @throws IOException * if the requested program can not be executed. */ public Process exec(String prog, String[] envp, File directory) throws java.io.IOException { // Sanity checks if (prog == null) { throw new NullPointerException("prog == null"); } else if (prog.isEmpty()) { throw new IllegalArgumentException("prog is empty"); } // Break down into tokens, as described in Java docs StringTokenizer tokenizer = new StringTokenizer(prog); int length = tokenizer.countTokens(); String[] progArray = new String[length]; for (int i = 0; i < length; i++) { progArray[i] = tokenizer.nextToken(); } // Delegate return exec(progArray, envp, directory); }prog时需要执行的程序,怎么理解,其实很简单,就是系统的command命令,到这大家应该都清楚了,Android是Linux系统,这里的指令也就是Linux的命令了,例如ps(查看进程),ls,pwd(查看现在路径,文件等等)等等,大家都可以去试试。
1、exec的String prog只能执行一条指令,如果需要执行多条指令需要放入sh脚本文件中执行或者调用多次exec函数。
2、exec的String command不支持通配符(*)。
3、有些指令需要指定File dir(工作目录)。
4、默认的工作目录是根目录/。(通过执行指令pwd可查到)
5、导出logcat日志到SD卡ziyouku目录,参数:("logcat -df logcat.log",new File("/mnt/sdcard/ziyouku/"))
6、logcat读取权限:<uses-permission android:name="android.permission.READ_LOGS" />
好,今天就说这么多,最近在做Spingboot开发后台并发系统,所以写博客也是对之前项目的很多技术点的总结完善。