分析RunTime执行命令以及得到返回值
2016-10-29 10:00
369 查看
RunTime执行命令得到返回值
我们有在好好几篇博客里提到过RunTime,比如
JAVA之旅(二十三)——System,RunTime,Date,Calendar,Math的数学运算
Android源码浅析(五)——关于定制系统,如何给你的Android应用系统签名
而今天同样的,来聊聊RunTime,我们执行这些命令的时候获取到我们的返回值,实际上是比较简单的,但是RunTime的局限性也有点大,很多都没有权限。我们接着看,我现在在终端输入
adb version
看下会输出什么
那我问你,在Android中我们应该怎么去做?我们简单的分析一下,首先,最简单的就是执行语句了
Runtime.getRuntime().exec(cmd);
但是他的工作原理是什么呢?我们都没多大的了解,那我们就去源码里看看
public Process exec(String command) throws IOException { return exec(command, null, null); }
他实际上执行的是exec的本类方法,我们继续看:
public Process exec(String command, String[] envp, File dir) throws IOException { if (command.length() == 0) throw new IllegalArgumentException("Empty command"); StringTokenizer st = new StringTokenizer(command); String[] cmdarray = new String[st.countTokens()]; for (int i = 0; st.hasMoreTokens(); i++) cmdarray[i] = st.nextToken(); return exec(cmdarray, envp, dir); }
他这里重载了多个exec,我们继续追踪,但是可以肯定是的是,他的返回时一个Process 对象,好的,继续追我们可以看到
public Process exec(String[] cmdarray, String[] envp, File dir) throws IOException { return new ProcessBuilder(cmdarray) .environment(envp) .directory(dir) .start(); }
他最终是new了一个ProcessBuilder去start执行,这里就不往下继续追了,可以看出,他在多个地方有I/O异常,这足以说明了一点,就是他是关于流的操作,那我们肯定是可以我们是能拿到流的,那我们可以直接get了
Process p = Runtime.getRuntime().exec(cmd); InputStream is = p.getInputStream();
我们看他的源码里知道他的Process 是可以拿到流,那我们尝试一下就能拿到InputStream 先试着去读取一下,那我们的执行代码应该就是这样写:
//执行 private void playRunTime() throws Exception { String cmd = "adb version"; Process p = Runtime.getRuntime().exec(cmd); InputStream is = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { tv_result.append(line + "\n"); } p.waitFor(); is.close(); reader.close(); p.destroy(); }
把这段代码执行下看下是否是能拿到结果:
果不其然,是能拿到的,那样就很好理解RunTime了,一场关于I和O的战斗即将展开,我们看看其他的命令
ls -l
反复的测试了一下,也验证了RunTime了其实局限性还是有的,不过大多数是权限的问题,如果有系统的签名文件的话,那就比较顺利了,不然就只能使用一些简单的命名做做Demo ,而关机,关机什么的,也是需要Root权限的,关于开关机,可以参考我的这篇Blog:
Android实训案例(二)——Android下的CMD命令之关机重启以及重启recovery
到这里,本片算是OK了,简单的分析了一下过程,Demo就不上传了,直接贴上代码
layout_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="程序启动...." android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="@color/colorPrimary" android:textSize="18sp"/> </ScrollView> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_input_add"/> </RelativeLayout>
MainActivity
package com.liuguilin.runtimesample; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.TextView; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { public static final String TAG = MainActivity.class.getSimpleName(); private TextView tv_result; private FloatingActionButton fab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } //初始化 private void initView() { tv_result = (TextView) findViewById(R.id.tv_result); tv_result.append("\n"); fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { playRunTime("ls -l"); } catch (Exception e) { Log.e(TAG, e.toString()); } } }); } //执行 private void playRunTime(String cmd) throws Exception { Process p = Runtime.getRuntime().exec(cmd); InputStream is = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { tv_result.append(line + "\n"); } p.waitFor(); is.close(); reader.close(); p.destroy(); } }
有兴趣的加群:555974449,我们继续聊聊人生
相关文章推荐
- 分析RunTime执行命令以及得到返回值
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- Hibernate SQLQuery查询返回空List,在mysql命令下直接执行sql语句可以正常得到记录
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 在C/python中执行linux命令并得到返回值以及输出
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 在C/python中执行linux命令并得到返回值以及输出
- JAVA调用系统命令或可执行程序--返回一个Runtime运行时对象,然后启动另外一个进程来执行命令
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 数 4000 据库备份 runtime.exec 去执行命令 返回 process 读取process的输入流 把数据写入执行文件里
- 在程序中调用外部程序,用process返回命令执行结果以及抓取错误信息的方法
- 开关机自动执行脚本方法[以及切换用户执行命令方法]