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/");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值