Android: 通过Runtime.getRuntime().exec调用底层Linux下的程序或脚本
Android Runtime使得直接调用底层Linux下的可执行程序或脚本成为可能
比如Linux下写个测试工具,直接编译后apk中通过Runtime来调用
或者写个脚本,apk中直接调用,省去中间层或者JNI
这个至少效率应该比较高吧



代码:
public class test extends Activity {
TextView text; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); text = (TextView) findViewById(R.id.text); Button btn_ls = (Button) findViewById(R.id.btn_ls);
btn_ls.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
do_exec("ls /mnt/sdcard");
}
});
Button btn_cat = (Button) findViewById(R.id.btn_cat);
btn_cat.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
do_exec("cat /proc/version");
}
});
Button btn_rm = (Button) findViewById(R.id.btn_rm);
btn_rm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
do_exec("rm /mnt/sdcard/1.jpg");
}
});
Button btn_sh = (Button) findViewById(R.id.btn_sh);
btn_sh.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
do_exec("/system/bin/sh /mnt/sdcard/test.sh 123");
}
});
} String do_exec(String cmd) {
String s = "/n";
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
s += line + "/n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text.setText(s);
return cmd;
}
}
test.sh:
echo test.sh
echo $1
需要注意:
1. exec不等于console命令
2. exec的输入输出流需要自己处理
3. exec执行时阻塞、非阻塞,返回结果问题
4. 注意权限问题
有个文章讲的比较深入,贴出来研究:
http://blog.csdn.net/zmyde2010/archive/2011/01/12/6130895.aspx
http://kuangbaoxu.javaeye.com/blog/210291
Android: 通过Runtime.getRuntime().exec调用底层Linux下的程序或脚本的更多相关文章
- Runtime.getRuntime.exec()执行linux脚本导致程序卡死有关问题
Runtime.getRuntime.exec()执行linux脚本导致程序卡死问题问题: 在Java程序中,通过Runtime.getRuntime().exec()执行一个Linux脚本导致程序被 ...
- 使用Runtime.getRuntime().exec()在java中调用python脚本
举例有一个Python脚本叫test.py,现在想要在Java里调用这个脚本.假定这个test.py里面使用了拓展的包,使得pythoninterpreter之类内嵌的编译器无法使用,那么只能采用ja ...
- [转]java调用外部程序Runtime.getRuntime().exec
Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: public Process exec( ...
- JAVA调用可执行程序或系统命令Runtime.getRuntime().exec
用Java编写应用时,有时需要在程序中调用另一个现成的可执行程序或系统命令,这时可以通过组合使用Java提供的Runtime类和Process类的方法实现.下面是一种比较典型的程序模式: Proces ...
- Runtime.getRuntime().exec()实现Java调用python程序
使用Runtime.getRuntime().exec()来实现Java调用python,调用代码如下所示: import java.io.BufferedReader; import java.io ...
- Android Runtime.getRuntime().exec
try { // Executes the command. Process process = Runtime.getRuntime().exec(cmd); // NOTE: You can wr ...
- 用Runtime.getRuntime().exec()需要注意的地方
有时候我们可能需要调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用,他会生成一个新的进程去运行调用的程序. 此方法返回一个java.lang.Proce ...
- Runtime.getRuntime().exec中命令含有括号问题
在写批量运行bat工具的时候.想起了之前写的定时小工具里面的运行方法. 使用Runtime.getRuntime().exec方法. Runtime.getRuntime().exec("c ...
- 使用Runtime.getRuntime().exec()方法的几个陷阱 (转)
Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法 ...
随机推荐
- SxsTrace使用的方法
Windows7平台上有一个强大的SxsTrace工具,可以跟踪调试应用程序运行时需要的动态库的版本和路径. SxsTrace使用的方法: 1.首先必须以Administrator用户身份登录 ...
- Django不能使用ip方式访问的解决办法
问题: 启动服务后,使用http://127.0.0.1:8000/showcase/或者http://localhost:8000/showcase/都能访问, 但是使用http://192.168 ...
- angular 辅助路由
- SOA IN Real World
微软发布了一个名为“真实世界里的面向服务架构(SOA)”的电子书.这本书表达了微软对面向服务架构的观点,并包括了数个展示如何用微软产品和技术实现SOA的真实案例.书中解释到,SOA的功能型架构本身是松 ...
- lower_bound下确界
//lower_bound用于找到首个大于等于某个值的元素 #include<algorithm> #include<iostream> using namespace std ...
- Mongoid Relationships
1:1 embedded relationship: M:1 linked relationship(one_direction): child uses belongs_to parent uses ...
- c语言指针的简单实例
c语言的指针的存在使得c语言对硬件的操控,以及灵活性得到了极大的提高. 但是指针的使用存在着很多难点问题. #include<stdlib.h> #include<stdio.h&g ...
- loj #107. 维护全序集
#107. 维护全序集 题目描述 这是一道模板题,其数据比「普通平衡树」更强. 如未特别说明,以下所有数据均为整数. 维护一个多重集 S SS ,初始为空,有以下几种操作: 把 x xx 加入 S S ...
- 更改Linux下的时间
1.使用tzseletect glibc-common-2.12-1.192.el6.x86_64 : Common binaries and locale data for glibc Repo : ...
- 如何在UITableViewController上添加一个固定的视图
最近在使用UITableViewController,想在上面添加一个固定的视图,不随ScrollView滑动而移动.最后找到2种解决办法,一种是计算TableView的偏移,调整视图的位置,不断更新 ...