当前位置:   article > 正文

Android - 代码执行shell命令的应用_android执行shell命令 不返回结果

android执行shell命令 不返回结果


前言

本文只上代码,没有什么好讲的,直接复制粘贴拿去用就行了


一、执行命令无返回

    /*
    执行命令无返回
    用法:execSuCmd("logcat -c")
     */
    public void execSuCmd(String cmd) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("sh");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            os.close();
            process.destroy();
        } catch (Exception e) {
        } finally {
            try {
                if (os != null) os.close();
            } catch (IOException e) {
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

二、执行命令并接收返回结果

    /*
        执行命令并接收返回结果,执行失败返回失败打印,执行成功返回成功打印
        用法:execCommand("ifconfig")
     */
    public String execCommand(String cmd) {
        String result = "";
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("sh");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes("exit\n");
            os.flush();

            char[] buff = new char[1024];
            int ch = 0;

            //命令执行成功,接收输出内容
            BufferedReader bfsd = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuffer sbs = new StringBuffer();
            while ((ch = bfsd.read(buff)) != -1) {
                sbs.append(buff, 0, ch);
            }
            bfsd.close();
            //命令执行失败,接收错误信息
            BufferedReader ero = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            StringBuffer era = new StringBuffer();
            while ((ch = ero.read(buff)) != -1) {
                era.append(buff, 0, ch);
            }
            ero.close();
            os.close();
            process.destroy();

            if (sbs.length() != 0) {
                result = String.valueOf(sbs);
            } else {
                result = String.valueOf(era);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

三、查询是否有指定的节点

    /*
    查询是否有指定的节点,没有则返回-1
    用法:getATTRData("cat /sys/devices/platform/ff140000.i2c/i2c-5/5-0027/PCF8574_TO_ZigbeeGPIO5");
    */
    public int getATTRData(String sys_path) {
        int ret = -1;
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(sys_path);
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            while (null != (line = br.readLine())) {
                ret = Integer.parseInt(line);
                return ret;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

四、查询系统中指定路径的文件是否存在

   /*
    查询系统中指定路径的文件是否存在
    用法:ttyPathExist("/sys/devices/platform/fec80000.i2c/i2c-6/6-0062/testLED");
    */
    public boolean ttyPathExist(String path) {
        File ttyPath_ok = new File(path);
        boolean exist = ttyPath_ok.exists();
        if (exist)
            return true;
        else
            return false;
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

五、执行命令后循环接收返回内容

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button on_log;
    private Button off_log;
    private TextView tx;
    private ScrollView slv;
    private String reslut = null;
    private Process process = null;
    private DataOutputStream os = null;
    private BufferedReader bfsd = null;
    private boolean onoff = false;
    private Handler myHander = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 111) {
                tx.setText(reslut);
                slv.fullScroll(ScrollView.FOCUS_DOWN);
            }
        }

    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.on_log:
                //打开接收
                onoff = true;
             //   threaadExecSuCmd("screenrecord /sdcard/demo.mp4");
             //   threaadExecSuCmd("ifconfig");
                threaadExecSuCmd("logcat");
                break;
            case R.id.off_log:
                //关闭接收
                onoff = false;
                break;
        }
    }

    public void threaadExecSuCmd(String cmd) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    process = Runtime.getRuntime().exec("su");
                    os = new DataOutputStream(process.getOutputStream());
                    os.writeBytes(cmd + "\n");
                    os.writeBytes("exit\n");
                    os.flush();
                    bfsd = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    StringBuffer sbs = new StringBuffer();
                    char[] buff = new char[1024];
                    int ch = 0;
                    while ((ch = bfsd.read(buff)) != -1 && onoff) {
                        sbs.append(buff, 0, ch);
                        reslut = String.valueOf(sbs);
                        myHander.sendEmptyMessage(111);
                    }
                    bfsd.close();
                    os.close();
                    process.destroy();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        on_log = (Button) findViewById(R.id.on_log);
        off_log = (Button) findViewById(R.id.off_log);
        tx = (TextView) findViewById(R.id.tx);

        on_log.setOnClickListener(this);
        off_log.setOnClickListener(this);
        slv = (ScrollView) findViewById(R.id.slv);
        slv.setOnClickListener(this);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

总结

部分shell命令需要系统签名后才能使用,比如下滑通知栏的命令

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/733989
推荐阅读
相关标签
  

闽ICP备14008679号