如何通过Android游戏运行多个shell命令 [英] How to run multiple shell commands through an app in android

查看:141
本文介绍了如何通过Android游戏运行多个shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的应用程序运行3个不同的命令,但只有第一个是越来越执行。
这里的code。

 工艺过程=调用Runtime.getRuntime()EXEC(苏);
。过程=调用Runtime.getRuntime()EXEC(安装邻重新装载,RW /系统);
。过程=调用Runtime.getRuntime()EXEC(CP / SD卡/主机/系统的/ etc);

我得到的root访问权限,但之后没有什么事情发生。

编辑:我想这code但这也只执行苏COMAND

 的String [] =命令{安装邻重新装载,RW /系统,CP / SD卡/主机/系统的/ etc};                        进程p =调用Runtime.getRuntime()EXEC(苏);
                        DataOutputStream类OS =新的DataOutputStream类(p.getOutputStream());
                        对于(字符串tmpCmd:命令){
                                os.writeBytes(tmpCmd +\\ n);
                        }
                        os.writeBytes(退出\\ n);
                        os.flush();

编辑:这工作,但只有一个time命令,我不得不做出一个按钮,每一个命令

 的String [] = HIN1 {苏,-c,CP / SD卡/主人MediaFire /主机/系统的/ etc /};
                     尝试{
                         调用Runtime.getRuntime()EXEC(HIN1)。
                     }赶上(IOException异常五){
                         // TODO自动生成catch块
                         e.printStackTrace();
                     }


解决方案

这是容易做到的。

使用根的工具。

从这个链接添加jar文件:
https://github.com/Stericson/RootTools

 命令命令=新的Command(0,回声这是命令,回声这是另一个命令){
        @覆盖
        公共无效输出(INT ID,串线)
        {
            //做一些与此输出
        }
};
RootTools.getShell(真)。新增(命令).waitForFinish();

I am trying to run 3 different commands through my app but only the first one is getting executed. here's the code.

Process process = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec("mount -o remount,rw /system");
process = Runtime.getRuntime().exec("cp /sdcard/hosts /system/etc");

I get the root access but after that nothing else happens.

EDIT: I tried this code but this also executes only su comand

String[] commands = {"mount -o remount,rw /system", "cp /sdcard/hosts /system/etc"};

                        Process p = Runtime.getRuntime().exec("su");
                        DataOutputStream os = new DataOutputStream(p.getOutputStream());            
                        for (String tmpCmd : commands) {
                                os.writeBytes(tmpCmd+"\n");
                        }           
                        os.writeBytes("exit\n");  
                        os.flush();

EDIT: This works but only one command at time, i'll have to make a button for every command.

String[] hin1 = { "su", "-c","cp /sdcard/Mediafire/hosts /system/etc/" };
                     try {
                         Runtime.getRuntime().exec(hin1);
                     } catch (IOException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                     }

解决方案

This is easy to do.

Use "root tools."

Add the jar file from this link: https://github.com/Stericson/RootTools.

Command command = new Command(0, "echo this is a command", "echo this is another command"){
        @Override
        public void output(int id, String line)
        {
            //Do something with the output here
        }
};
RootTools.getShell(true).add(command).waitForFinish();

这篇关于如何通过Android游戏运行多个shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆