一、方法
复制代码 代码如下:
/**
*执行一个shell命令,并返回字符串值
*
*@paramcmd
*命令名称&参数组成的数组(例如:{"/system/bin/cat","/proc/version"})
*@paramworkdirectory
*命令执行路径(例如:"system/bin/")
*@return执行结果组成的字符串
*@throwsIOException
*/
publicstaticsynchronizedStringrun(String[]cmd,Stringworkdirectory)
throwsIOException{
StringBufferresult=newStringBuffer();
try{
//创建操作系统进程(也可以由Runtime.exec()启动)
//Runtimeruntime=Runtime.getRuntime();
//Processproc=runtime.exec(cmd);
//InputStreaminputstream=proc.getInputStream();
ProcessBuilderbuilder=newProcessBuilder(cmd);
InputStreamin=null;
//设置一个路径(绝对路径了就不一定需要)
if(workdirectory!=null){
//设置工作目录(同上)
builder.directory(newFile(workdirectory));
//合并标准错误和标准输出
builder.redirectErrorStream(true);
//启动一个新进程
Processprocess=builder.start();
//读取进程标准输出流
in=process.getInputStream();
byte[]re=newbyte[1024];
while(in.read(re)!=-1){
result=result.append(newString(re));
}
}
//关闭输入流
if(in!=null){
in.close();
}
}catch(Exceptionex){
ex.printStackTrace();
}
returnresult.toString();
}
二、用途
执行Linux下的top、ps等命令,这些命令你也通过adb可以执行查看效果。
1)top命令如下:
复制代码 代码如下:
adbshell
$top-h
top-h
Usage:top[-mmax_procs][-niterations][-ddelay][-ssort_column][-t][-h]
-mnumMaximumnumberofprocessestodisplay.//最多显示多少个进程
-nnumUpdatestoshowbeforeexiting.//刷新次数
-dnumSecondstowaitbetweenupdates.//刷新间隔时间(默认5秒)
-scolColumntosortby<cpu,vss,rss,thr>//按哪列排序
-tShowthreadsinsteadofprocesses.//显示线程信息而不是进程
-hDisplaythishelpscreen.//显示帮助文档
$top-n1
top-n1
就不把执行效果放上来了,总之结果表述如下:
复制代码 代码如下:
User35%,System13%,IOW0%,IRQ0%//CPU占用率
User109+Nice0+Sys40+Idle156+IOW0+IRQ0+SIRQ1=306//CPU使用情况
PIDCPU%S#THRVSSRSSPCYUIDName//进程属性
xxxx%xxxxxxxxxxxxx
CPU占用率:
User用户进程
System系统进程
IOWIO等待时间
IRQ硬中断时间
CPU使用情况(指一个最小时间片内所占时间,单位jiffies。或者指所占进程数):
User处于用户态的运行时间,不包含优先值为负进程
Nice优先值为负的进程所占用的CPU时间
Sys处于核心态的运行时间
Idle除IO等待时间以外的其它等待时间
IOWIO等待时间
IRQ硬中断时间
SIRQ软中断时间
进程属性:
PID进程在系统中的ID
CPU%当前瞬时所以使用CPU占用率
S进程的状态,其中S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值是负数。
#THR程序当前所用的线程数
VSSVirtualSetSize虚拟耗用内存(包含共享库占用的内存)
RSSResidentSetSize实际使用物理内存(包含共享库占用的内存)
PCYOOXX,不知道什么东东
UID运行当前进程的用户id
Name程序名称android.process.media
//ps:内存占用大小有如下规律:VSS>=RSS>=PSS>=USS
//PSSProportionalSetSize实际使用的物理内存(比例分配共享库占用的内存)
//USSUniqueSetSize进程独自占用的物理内存(不包含共享库占用的内存)
在附件Android系统->android top.txt文件内,自个总结的。
2)执行代码
复制代码 代码如下:
//top命令
publicstaticfinalString[]TOP={"/system/bin/top","-n","1"};
//现在执行top-n1,我们只需要第二行(用第二行求得CPU占用率,精确数据)
//第一行:User35%,System13%,IOW0%,IRQ0%//CPU占用率
//第二行:User109+Nice0+Sys40+Idle156+IOW0+IRQ0+SIRQ1=306
////CPU使用情况
publicstaticsynchronizedStringrun(String[]cmd){
Stringline="";
InputStreamis=null;
try{
Runtimeruntime=Runtime.getRuntime();
Processproc=runtime.exec(cmd);
is=proc.getInputStream();
//换成BufferedReader
BufferedReaderbuf=newBufferedReader(newInputStreamReader(is));
do{
line=buf.readLine();
//前面有几个空行
if(line.startsWith("User")){
//读到第一行时,我们再读取下一行
line=buf.readLine();
break;
}
}while(true);
if(is!=null){
buf.close();
is.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
returnline;
}
//获取指定应用的top命令获取的信息
//PIDCPU%S#THRVSSRSSPCYUIDName//进程属性
//如果当前应用不在运行则返回null
publicstaticsynchronizedStringrun(String[]cmd,StringpkgName){
Stringline=null;
InputStreamis=null;
try{
Runtimeruntime=Runtime.getRuntime();
Processproc=runtime.exec(cmd);
is=proc.getInputStream();
//换成BufferedReader
BufferedReaderbuf=newBufferedReader(newInputStreamReader(is));
do{
line=buf.readLine();
//读取到相应pkgName跳出循环(或者未找到)
if(null==line||line.endsWith(pkgName)){
break;
}
}while(true);
if(is!=null){
buf.close();
is.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
returnline;
}