RunTime执行命令得到返回值


我们有在好好几篇博客里提到过RunTime,比如

而今天同样的,来聊聊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:

到这里,本片算是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执行命令以及得到返回值的更多相关文章

  1. cocos打包出现错误,执行命令出错,返回值:2。 Traceback (most recent call last): File "E:\cocos_workspace\MyGameOne\proj.android\build_native.py", line 43, in <module> build(opts.build_mode) File "E:\cocos_workspace\MyGa

    先看看NDK的版本,如果不行,就删除\proj.android\obj\local\armeabi下的文件.

  2. python 利用python的subprocess模块执行外部命令,获取返回值

    有时执行dos命令需要保存返回值 需要导入库subprocess import subprocess p = subprocess.Popen('ping www.baidu.com', shell= ...

  3. python执行系统命令后获取返回值的几种方式集合

    python执行系统命令后获取返回值的几种方式集合 今天小编就为大家分享一篇python执行系统命令后获取返回值的几种方式集合,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 第一种情 ...

  4. Python中调用Linux命令并获取返回值

    方法一.使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256/512表示未找到,该方法适用于she ...

  5. python执行系统命令后获取返回值

    import os, subprocess # os.system('dir') #执行系统命令,没有获取返回值,windows下中文乱码 # result = os.popen('dir') #执行 ...

  6. C++调用linux命令并获取返回值

    qt中封装了相关的方法, 但是因为我的命令中用到了管道命令, 出现了非预期结果, 所有改用了linux系统原生的方法. 下边是一个判断某进程是否存在的例子. 当前存在一个问题,当linux返回多行时, ...

  7. 【java】并发执行ExecutorService的sumbit返回值的顺序问题

    ArrayList<Future> fl = new ArrayList<Future>(); for (int i = 0; i < 10; i++) { Future ...

  8. sp_executesql得到执行sql语句的返回值

    执行 sql语句,得到 变量的值 ' declare @Partition int; ); ); SET @SQLString = N'SELECT @RangeKeyOUT = $PARTITION ...

  9. ASP.NET 成功执行Update 的 ExecuteNonQuery() 返回值大于0,但是查看数据库却没有改变

    //真实姓名保存 $("#TrueNameSaveBtn").click(function () { if ($("#TrueNameSaveText").va ...

随机推荐

  1. Linux OpenGL 实践篇-2 创建一个窗口

    OpenGL 作为一个图形接口,并没有包含窗口的相关内容,但OpenGL使用必须依赖窗口,即必须在窗口中绘制.这就要求我们必须了解一种窗口系统,但不同的操作系统提供的创建窗口的API都不相同,如果我们 ...

  2. further occurrences of HTTP header parsing errors will be logged at DEBUG level.错误

    今天进行项目测试的时候出现了further occurrences of HTTP header parsing errors will be logged at DEBUG level.错误,查了半 ...

  3. 第一章 搭建一个通用的.net core项目框架

    项目目标部署环境:CentOS 7+ 项目技术点:.netcore2.0 + Autofac +webAPI + NHibernate5.1 + mysql5.6 + nginx 开源地址:https ...

  4. [LeetCode] Couples Holding Hands 两两握手

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  5. [LeetCode] Valid Palindrome II 验证回文字符串之二

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  6. slf4j 与各个 logging框架的适配器说明

    在java领域里,日志框架纷杂繁多,项目中必然要使用很多的第三方库,而这些第三方库所使用的log框架又不尽相同.想要打出合理有效的日志,就必须在你的项目中将这些日志框架统一才行.幸好,slf4j, c ...

  7. [POI 2007]ZAP-Queries

    Description Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency) ...

  8. 洛谷P1593 因子和

    题目描述 输入两个正整数a和b,求a^b的因子和.结果太大,只要输出它对9901的余数. 输入输出格式 输入格式: 仅一行,为两个正整数a和b(0≤a,b≤50000000). 输出格式: a^b的因 ...

  9. HDU2425:Hiking Trip(BFS+优先队列)

    给出一个地图,地图有四种路面,经过每种路面花费的时间不同,问从起点到终点所花费的最少时间是多少 把到各个点的花费存入队列中,然后弹出,即可得到最小 Sample Input 4 6 1 2 10 T. ...

  10. 【AHOI2005】病毒检测

    题目描述 科学家们在Samuel星球上的探险仍在继续.非常幸运的,在Samuel星球的南极附近,探险机器人发现了一个巨大的冰湖!机器人在这个冰湖中搜集到了许多RNA片段运回了实验基地. 科学家们经过几 ...