在android执行shell命令

               

http://www.cnblogs.com/zhengwenwei/archive/2011/08/16/2141642.html

android应用需要调用shell命令的时候,网上找到的资料是:

复制代码
        String cmd = String.format("echo %s > %s\n", arg, mSwitchUsbFileTextEdit.getText().toString());
try {
            Process exeEcho = Runtime.getRuntime().exec("su");
            exeEcho.getOutputStream().write(cmd.getBytes());
            exeEcho.getOutputStream().flush();
        } catch (IOException e) {
            showMessage("Excute exception: " + e.getMessage());
        }
复制代码

  注意,之所以需要执行su命令,而不是直接执行echo命令,是因为那样会跑出IOException: Working Directory: null    Environment: null。但是如果是在装有SuperRoot应用的机器上,su命令可能被拒绝,而且执行普通的命令应该不需要使用su命令才对。试验了一下,Process exeEcho = Runtime.getRuntime().exec("su");这一行改成Process exeEcho = Runtime.getRuntime().exec("sh");也可以执行。查了一下资料:

In many ways, each Android application lives in its own world:

  • By default, every application runs in its own Linux process. Android starts the process when any of the application's code needs to be executed, and shuts down the process when it's no longer needed and system resources are required by other applications.
  • Each process has its own Java virtual machine (VM), so application code runs in isolation from the code of all other applications.
  • By default, each application is assigned a unique Linux user ID. Permissions are set so that the application's files are visible only that user, only to the application itself — although there are ways to export them to other applications as well.

It's possible to arrange for two applications to share the same user ID, in which case they will be able to see each other's files. To conserve system resources, applications with the same ID can also arrange to run in the same Linux process, sharing the same VM.

是否因为这个应用的用户是一个"不完全"的用户,所以需要切换到shell用户才能执行命令?


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值