android Runtime.getRuntime().exec使用

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1

http://blog.csdn.net/zmyde2010/article/details/6123987

http://search.iteye.com/blog/253257



1.read value:

 public String readRegister(String path){
		 try {
 
				Process process = Runtime.getRuntime().exec("/system/bin/cat  "+path);
 
			    BufferedReader reader = new BufferedReader(
			            new InputStreamReader(process.getInputStream()));
			    int read;
			    char[] buffer = new char[4096];
			    StringBuffer output = new StringBuffer();
			    while ((read = reader.read(buffer)) > 0) {
			        output.append(buffer, 0, read);
			    }
			    reader.close();
			    
			    // Waits for the command to finish.
			    process.waitFor();			    
			    
			    System.out.println(output.toString());
			    valRegister = output.toString();
			    return valRegister;
			    
			} catch (IOException e) {
			    throw new RuntimeException(e);
			} catch (InterruptedException e) {
			    throw new RuntimeException(e);
			}		
     }

2.change mode:  

Runtime.getRuntime().exec("su -c chmod 777 /data/glad.txt");

3.write value:

Runtime.getRuntime().exec("/system/bin/sh /data/test.sh");
test.sh:
echo 777 > /data/glad.txt
ps:  You cannot use Runtime.exec() like a command line
你不能用Runtime.exec()来执行像命令行那样的程序,如重定向输出:Process proc = rt.exec("java jecho 'Hello World' > test.txt");  
这段程序的意图是运行一个名为jecho的Java程序,并将其输出重定向到test.txt文件中。 但这种写法得不到正确结果,因为Runtime.exec()方法并不能很好的执行这种较为复杂的命令行,必须通过自己处理程序输出并写入到相应的文件


ps:

   写操作需要设置apk为系统应用。


你可能感兴趣的:(android Runtime.getRuntime().exec使用)