Runtime.getRuntime().exec()----记录日志案例

Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令。
Runtime.getRuntime().exec共有六个重载方法:
1.public Process exec(String command)
在单独的进程中执行指定的字符串命令。
2.public Process exec(String [] cmdArray)
在单独的进程中执行指定命令和变量
3.public Process exec(String command, String [] envp)
在指定环境的独立进程中执行指定命令和变量
4.public Process exec(String [] cmdArray, String [] envp)
在指定环境的独立进程中执行指定的命令和变量
5.public Process exec(String command,String[] envp,File dir)
在有指定环境和工作目录的独立进程中执行指定的字符串命令
6.public Process exec(String[] cmdarray,String[] envp,File dir)
在指定环境和工作目录的独立进程中执行指定的命令和变量

我们先来比较exec(String command)与exec(String[] cmdArray)的区别,其实他们是等价的,最终都会调用:
exec(String[] cmdarray,String[] envp,File dir),我们看看方法exec(String cmdarray,String[] envp,File dir) throws IOException的实现代码:
public Process exec(String command, String[] envp, File dir) throws IOException {
    if (command.length() == 0) throw new IllegalArgumentException("Empty command");
    StringTokenizer st = new StringTokenizer(command);
    String[] cmdarray = new String[st.countTokens()];
    for (int i = 0; st.hasMoreTokens(); i++)
        cmdarray[i] = st.nextToken();
    return exec(cmdarray, envp, dir);
}

从上面的代码,我们可以看出最终调用的代码都是:exec(String[] cmdArray,String envp,File  dir)。exec(String command)相当于exec(command,null,null),exec(String[] cmdArray)相当于exec(cmdArray,null,null)。


参数说明:
cmdarray - 包含所调用命令及其参数的数组。
envp - 字符串数组,其中每个元素的环境变量的设置格式为 name=value,如果子进程应该继承当前进程的环境,或该参数为 null。
dir - 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为 null。

另外,执行exec(String command)不等同于直接执行command line命令,比如命令:
javap -l xxx > output.txt
这时要用exec(String[] cmdArray)。如例:
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",

    "javap -l xxx > output.txt"});


关于返回结果类型:Process,它有几个方法:
1.destroy():杀掉子进程
2.exitValue():返回子进程的出口值,值 0 表示正常终止
3.getErrorStream():获取子进程的错误流
4.getInputStream():获取子进程的输入流

5.getOutputStream():获取子进程的输出流

6.waitFor():导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。如果已终止该子进程,此方法立即返回。如果没有终止该子进程,调用的线程将被阻塞,直到退出子进程,根据惯例,0 表示正常终止


对Runtime.getRuntime.exec()的了解可以参考博客:http://www.cnblogs.com/mingforyou/p/3551199.html

案列:实现Log中的日志显示(以对话框的形式演示,可以将输出日志保存到具体的文件中)

public class MainActivity extends Activity implements OnClickListener{
    Button conLog;
	/** Called when the activity is first created. */
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        conLog = (Button) findViewById(R.id.conLog);//通过id找到的按钮
        MyLog.isDebug = true;//debug模式开启
       conLog.setOnClickListener(this);
    }
	public void onClick(View v) {
		//按钮被点击到了,收集收集日志
		try {
			readLog();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @author Danny QQ 858030348  
	 * @throws IOException 
	 * 
	 */
	private void readLog() throws IOException {
		MyLog.i("INFO", "start connectLog");
		StringBuffer sb = new StringBuffer();
		ArrayList<String> cmdLine = new ArrayList<String>();
		cmdLine.add("logcat");
		cmdLine.add("-d");//收集一次日志停止
		cmdLine.add("-s");//过滤
		cmdLine.add("INFO");
		System.out.println(cmdLine.toArray(new String[cmdLine.size()]));
		Process exec = Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()]));
		//获取执行命令后的输入流
		InputStream inputStream = exec.getInputStream();
		InputStreamReader buInputStreamReader = new InputStreamReader(inputStream);//装饰器模式
		BufferedReader bufferedReader = new BufferedReader(buInputStreamReader);//直接读字符串
		String str = null;
		while((str = bufferedReader.readLine())!=null){
			sb.append(str);//每读一行拼接到sb里面去
			sb.append("\n");//每一行一个换行符
		}
		//吐司
		Toast.makeText(this, sb.toString(), 1000).show();
	}
}
对于Logcat的命令可以参考博客:http://www.jb51.net/article/47055.htm

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值