分析RunTime执行命令以及得到返回值
来源:互联网 发布:windows live mail pst 编辑:程序博客网 时间:2024/06/16 18:20
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执行命令以及得到返回值
- 在C/python中执行linux命令并得到返回值以及输出
- 在C/python中执行linux命令并得到返回值以及输出
- shell下执行一条命令,怎么得到返回值
- 执行字符串sql语句得到返回值
- 得到system执行后的返回值
- 在VC下执行DOS命令并得到返回结果
- java 调用 linux命令 得到返回值
- 从Alamofire中得到返回值 分析
- linux命令执行返回值
- linux命令执行返回值
- 使用ajax得到返回值时执行顺序问题
- java给线程传递参数以及得到返回值
- Android Runtime 返回值
- Java 执行运行时命令 Runtime
- java使用Runtime.exec执行linux命令
- java使用Runtime.exec执行linux命令
- 用Runtime.getRuntime().exec(cmd)执行命令
- 利用header下载图片等文件
- Mybatis学习总结五
- java图片开源框架tesseract调用OCR实现图片文件识别代码下载
- POJ 2226 二分图最小覆盖
- KMP算法
- 分析RunTime执行命令以及得到返回值
- Linux下 PostgreSQL 编译安装
- Ubuntu12.04 root 用户登录设置
- Hadoop学习笔记 压缩
- 解决问题:onreadystatechange只触发一次
- 安卓 组件Service知识理解
- C4.5决策树代码详细解析以及C4.5程序调用(正确的代码!!!)
- Android开发 音频的录制(MediaRecorder)与播放(MediaPlayer)踩坑记录
- iOS 初始化报错 cannot assign to self outside of a method in the init