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. java中IO操作

    File类 /** * 路径分隔符:public static final String separator; * 设置文件路径:public File(String pathName) ; * 创建 ...

  2. jquery checkbox勾选/取消勾选checked属性不生效问题

    http://www.cnblogs.com/KeenLeung/p/3799895.html

  3. [LeetCode] 1-bit and 2-bit Characters 一位和两位字符

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  4. [LeetCode] Largest Palindrome Product 最大回文串乘积

    Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...

  5. 众说纷纭的ul、ol、li

    (1)提到ul ol li,大家都知道,就是三个列表标签,ul表示无需列表(unordered list),ol表示有序列表(oredr list), li 表示列表项(list item),之前我也 ...

  6. “百度杯”CTF比赛 十二月场_blog(kindeditor编辑器遍历,insert注入,文件包含)

    题目在i春秋的ctf训练营中能找到 首先先是一个用户登录与注册界面,一般有注册界面的都是要先让你注册一波,然后找惊喜的 那我就顺着他的意思去注册一个号 注册了一个123用户登录进来看到有个文本编辑器, ...

  7. Caffe的运行mnist手写数字识别

    老规矩,首先附上官方教程:http://caffe.berkeleyvision.org/gathered/examples/mnist.html 1.必要软件 因为Caffe中使用的是Linux才能 ...

  8. [Codeforces 864E]Fire

    Description Polycarp is in really serious trouble — his house is on fire! It's time to save the most ...

  9. POJ2449 Remmarguts' Date

    "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...

  10. 【集训第三天·疯狂训练】哦,顺带学习了manacher

    虽然说是疯狂训练吧,但是也没写多少题,就把伸展树的操作熟悉了一下,ac了5个题目. 一整天没啥可吐槽的,除了昨天在机房打游戏的某位朋友翻车后和教练谈了谈心2333 说题吧.. 1.BZOJ1208 H ...