Process Command 执行命令行

	private String cmdExecute(String[] args, String workdirectory){
		ProcessBuilder cmd;
		String result = "";
		try {
			cmd = new ProcessBuilder(args);
			cmd.directory(new File(workdirectory));               
			cmd.redirectErrorStream(true);                 
			Process process = cmd.start();
			InputStream in = process.getInputStream();
			byte[] re = new byte[1024];
			int length = -1;
			while ((length = in.read(re)) != -1) {
				result = result + new String(re, 0, length);
			}
			in.close();
		} catch (IOException ex) {
			Log.i("cmdExecute",""+ex.getMessage());
		}
		return result;
	}

	/**
	 * command /proc/self/net/dev
	 * string format "%6s %13s %15s %14s %16s"
	 */
	public synchronized String getMobileDataUsage(){
		final String formater = "%6s %13s %15s %14s %16s";
		String reslut = "";
		reslut += String.format(formater,
				(Object[])new String[]{
										"type", 
										"receive-bytes", 
										"receive-packets", 
										"transmit-bytes",
										"transmit-packets"})
										+"\n";
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("/proc/self/net/dev"), 500);
			String line;
			br.readLine();
			br.readLine();
			while ((line = br.readLine()) != null) {
				String[] data = null;
				if (line != null || line.length() > 7 )
					data = line.substring(7).trim().split("\\s+");
				String[] res = new String[]{};
				if (data!=null && data.length >10)
					res = new String[]{line.substring(0,6),data[0],data[1],data[8],data[9]};
				reslut += String.format(formater, (Object[])res) + "\n";
			}
		} catch (IOException e) {
		} finally {
			try {
				if (br != null) 
					br.close();
			} catch (IOException e) {
			}
		}
		return reslut;
	}

	/**
	* command top -n 1
	*/
	public synchronized String displayCpuUtilization(){
		return cmdExecute(new String[]{"top", "-n", "1"},"/system/bin/");
	}
	/**
	* get memory info form ActivityManager service
	* 
	*/
	public synchronized String displayMemoryUtilization(){
		//return cmdExecute(new String[]{"/system/xbin/procrank"},"/system/bin/");
		ActivityManager am  =(ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
		Formatter titleformatter = new Formatter();
		String title = titleformatter.format("%5s%9s%9s  %s", 
				"PID","Pss","Uss","cmdline").toString();
		String info = "";
		for (RunningAppProcessInfo runningAppProcessInfo : list) {
			if (runningAppProcessInfo == null)
				continue;
			int[] pids = {runningAppProcessInfo.pid};
			android.os.Debug.MemoryInfo[] mee = am.getProcessMemoryInfo(pids);
			Formatter formatter = new Formatter();
			String s = formatter.format("%5s%8sK%8sK  %s", 
					runningAppProcessInfo.pid,
					mee[0].getTotalPss(),//PSS
					mee[0].getTotalPrivateDirty(),//USS
					//mee[0].getTotalSharedDirty(),
					runningAppProcessInfo.processName).toString();
			info = info + s + "\n";
			//Log.i("xx",s);
		}
		return title+"\n" +info;
	}
	
	/**
	* command /system/bin/df
	*/
	public synchronized String displayFlashUtilization(){
		return cmdExecute(new String[]{"/system/bin/df"},"/system/bin/");
	}

你可能感兴趣的:(command)