CMDExecute

package com.TaskManager;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class CMDExecute{
	/*
	 * args[0]:shell鍛戒护 濡�ls"鎴�ls -l";
	 * args[1]:鍛戒护鎵ц璺緞 濡�/";
	 */
	private String execute(String[] cmmand,String directory)throws IOException{
		String result="";
		ProcessBuilder builder=new ProcessBuilder(cmmand);
		if(directory!=null){
			builder.directory(new File(directory));
			builder.redirectErrorStream(true);
			Process process=builder.start();
			//寰楀埌shell鍛戒护,鎵ц鍚庣殑缁撴灉
			InputStream is=process.getInputStream();
			byte[] buffer=new byte[1024];
			while(is.read(buffer)!=-1){
				result=result+new String(buffer);
			}
			is.close();
		}
		return result;
	}
	//鎵ц"top -n l"鍛戒护
	public String getProcesRunningInfo(){
		String[] args={"/system/bin/top","-n","1"};
		String result="";
		try {
			result = execute(args, "/system/bin/");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	//鎵цkill鍛戒护
	public String killProcess(String pid){
		String[] args={"kill",pid,"9"};
		String result="";
		try {
			result=execute(args, "/system/bin/");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}

 

你可能感兴趣的:(execute)