在android执行shell命令
2013-01-12 23:41
344 查看
http://www.cnblogs.com/zhengwenwei/archive/2011/08/16/2141642.html
android应用需要调用shell命令的时候,网上找到的资料是:
注意,之所以需要执行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用户才能执行命令?
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用户才能执行命令?
相关文章推荐
- Android执行shell命令
- Android Java执行Shell命令
- Android App 获取root权限后,执行shell命令
- Android执行shell命令
- android 执行命令的shell工具类
- Android执行shell命令
- Android执行shell命令
- android 在手机sd 卡中执行linux 命令 和shell 脚本
- Android执行shell脚本命令(备忘记录与实际应用)
- Android或Java应用中如何以默认用户或root用户执行Shell命令
- Android执行执行Shell命令总结
- Android执行shell命令
- Android java代码中如何执行shell命令
- Android中级篇之用JAVA代码执行shell命令
- Android在代码中执行shell命令,针对root命令
- android执行shell命令,top 命令解析
- 在apk 中执行Runtime.getRuntime().exec adb shell各种命令远程控制其他Android设备(一)
- android java 执行shell命令(笔记)
- Android执行shell命令(top查看CPU信息)
- Android 执行 adb shell 命令