Android執(zhí)行shell命令詳解
更新時(shí)間:2013年06月15日 11:34:29 作者:
本篇文章是對(duì)Android執(zhí)行shell命令進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
一、方法
/**
* 執(zhí)行一個(gè)shell命令,并返回字符串值
*
* @param cmd
* 命令名稱&參數(shù)組成的數(shù)組(例如:{"/system/bin/cat", "/proc/version"})
* @param workdirectory
* 命令執(zhí)行路徑(例如:"system/bin/")
* @return 執(zhí)行結(jié)果組成的字符串
* @throws IOException
*/
public static synchronized String run(String[] cmd, String workdirectory)
throws IOException {
StringBuffer result = new StringBuffer();
try {
// 創(chuàng)建操作系統(tǒng)進(jìn)程(也可以由Runtime.exec()啟動(dòng))
// Runtime runtime = Runtime.getRuntime();
// Process proc = runtime.exec(cmd);
// InputStream inputstream = proc.getInputStream();
ProcessBuilder builder = new ProcessBuilder(cmd);
InputStream in = null;
// 設(shè)置一個(gè)路徑(絕對(duì)路徑了就不一定需要)
if (workdirectory != null) {
// 設(shè)置工作目錄(同上)
builder.directory(new File(workdirectory));
// 合并標(biāo)準(zhǔn)錯(cuò)誤和標(biāo)準(zhǔn)輸出
builder.redirectErrorStream(true);
// 啟動(dòng)一個(gè)新進(jìn)程
Process process = builder.start();
// 讀取進(jìn)程標(biāo)準(zhǔn)輸出流
in = process.getInputStream();
byte[] re = new byte[1024];
while (in.read(re) != -1) {
result = result.append(new String(re));
}
}
// 關(guān)閉輸入流
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result.toString();
}
二、用途
執(zhí)行Linux下的top、ps等命令,這些命令你也通過adb可以執(zhí)行查看效果。
1)top命令如下:
adb shell
$ top -h
top -h
Usage: top [-m max_procs] [-n iterations] [-d delay] [-s sort_column] [-t] [-h]
-m num Maximum number of processes to display. // 最多顯示多少個(gè)進(jìn)程
-n num Updates to show before exiting. // 刷新次數(shù)
-d num Seconds to wait between updates. // 刷新間隔時(shí)間(默認(rèn)5秒)
-s col Column to sort by <cpu,vss,rss,thr> // 按哪列排序
-t Show threads instead of processes. // 顯示線程信息而不是進(jìn)程
-h Display this help screen. // 顯示幫助文檔
$ top -n 1
top -n 1
就不把執(zhí)行效果放上來了,總之結(jié)果表述如下:
User 35%, System 13%, IOW 0%, IRQ 0% // CPU占用率
User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306 // CPU使用情況
PID CPU% S #THR VSS RSS PCY UID Name // 進(jìn)程屬性
xx xx% x xx xx xx xx xx xx
CPU占用率:
User 用戶進(jìn)程
System 系統(tǒng)進(jìn)程
IOW IO等待時(shí)間
IRQ 硬中斷時(shí)間
CPU使用情況(指一個(gè)最小時(shí)間片內(nèi)所占時(shí)間,單位jiffies?;蛘咧杆歼M(jìn)程數(shù)):
User 處于用戶態(tài)的運(yùn)行時(shí)間,不包含優(yōu)先值為負(fù)進(jìn)程
Nice 優(yōu)先值為負(fù)的進(jìn)程所占用的CPU時(shí)間
Sys 處于核心態(tài)的運(yùn)行時(shí)間
Idle 除IO等待時(shí)間以外的其它等待時(shí)間
IOW IO等待時(shí)間
IRQ 硬中斷時(shí)間
SIRQ 軟中斷時(shí)間
進(jìn)程屬性:
PID 進(jìn)程在系統(tǒng)中的ID
CPU% 當(dāng)前瞬時(shí)所以使用CPU占用率
S 進(jìn)程的狀態(tài),其中S表示休眠,R表示正在運(yùn)行,Z表示僵死狀態(tài),N表示該進(jìn)程優(yōu)先值是負(fù)數(shù)。
#THR 程序當(dāng)前所用的線程數(shù)
VSS Virtual Set Size 虛擬耗用內(nèi)存(包含共享庫占用的內(nèi)存)
RSS Resident Set Size 實(shí)際使用物理內(nèi)存(包含共享庫占用的內(nèi)存)
PCY OOXX,不知道什么東東
UID 運(yùn)行當(dāng)前進(jìn)程的用戶id
Name 程序名稱android.process.media
// ps:內(nèi)存占用大小有如下規(guī)律:VSS >= RSS >= PSS >= USS
// PSS Proportional Set Size 實(shí)際使用的物理內(nèi)存(比例分配共享庫占用的內(nèi)存)
// USS Unique Set Size 進(jìn)程獨(dú)自占用的物理內(nèi)存(不包含共享庫占用的內(nèi)存)
在附件Android系統(tǒng)->android top.txt文件內(nèi),自個(gè)總結(jié)的。
2)執(zhí)行代碼
// top命令
public static final String[] TOP = { "/system/bin/top", "-n", "1" };
// 現(xiàn)在執(zhí)行top -n 1,我們只需要第二行(用第二行求得CPU占用率,精確數(shù)據(jù))
// 第一行:User 35%, System 13%, IOW 0%, IRQ 0% // CPU占用率
// 第二行:User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306
// // CPU使用情況
public static synchronized String run(String[] cmd) {
String line = "";
InputStream is = null;
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
is = proc.getInputStream();
// 換成BufferedReader
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
do {
line = buf.readLine();
// 前面有幾個(gè)空行
if (line.startsWith("User")) {
// 讀到第一行時(shí),我們?cè)僮x取下一行
line = buf.readLine();
break;
}
} while (true);
if (is != null) {
buf.close();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
// 獲取指定應(yīng)用的top命令獲取的信息
// PID CPU% S #THR VSS RSS PCY UID Name // 進(jìn)程屬性
// 如果當(dāng)前應(yīng)用不在運(yùn)行則返回null
public static synchronized String run(String[] cmd, String pkgName) {
String line = null;
InputStream is = null;
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
is = proc.getInputStream();
// 換成BufferedReader
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
do {
line = buf.readLine();
// 讀取到相應(yīng)pkgName跳出循環(huán)(或者未找到)
if (null == line || line.endsWith(pkgName)) {
break;
}
} while (true);
if (is != null) {
buf.close();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
復(fù)制代碼 代碼如下:
/**
* 執(zhí)行一個(gè)shell命令,并返回字符串值
*
* @param cmd
* 命令名稱&參數(shù)組成的數(shù)組(例如:{"/system/bin/cat", "/proc/version"})
* @param workdirectory
* 命令執(zhí)行路徑(例如:"system/bin/")
* @return 執(zhí)行結(jié)果組成的字符串
* @throws IOException
*/
public static synchronized String run(String[] cmd, String workdirectory)
throws IOException {
StringBuffer result = new StringBuffer();
try {
// 創(chuàng)建操作系統(tǒng)進(jìn)程(也可以由Runtime.exec()啟動(dòng))
// Runtime runtime = Runtime.getRuntime();
// Process proc = runtime.exec(cmd);
// InputStream inputstream = proc.getInputStream();
ProcessBuilder builder = new ProcessBuilder(cmd);
InputStream in = null;
// 設(shè)置一個(gè)路徑(絕對(duì)路徑了就不一定需要)
if (workdirectory != null) {
// 設(shè)置工作目錄(同上)
builder.directory(new File(workdirectory));
// 合并標(biāo)準(zhǔn)錯(cuò)誤和標(biāo)準(zhǔn)輸出
builder.redirectErrorStream(true);
// 啟動(dòng)一個(gè)新進(jìn)程
Process process = builder.start();
// 讀取進(jìn)程標(biāo)準(zhǔn)輸出流
in = process.getInputStream();
byte[] re = new byte[1024];
while (in.read(re) != -1) {
result = result.append(new String(re));
}
}
// 關(guān)閉輸入流
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result.toString();
}
二、用途
執(zhí)行Linux下的top、ps等命令,這些命令你也通過adb可以執(zhí)行查看效果。
1)top命令如下:
復(fù)制代碼 代碼如下:
adb shell
$ top -h
top -h
Usage: top [-m max_procs] [-n iterations] [-d delay] [-s sort_column] [-t] [-h]
-m num Maximum number of processes to display. // 最多顯示多少個(gè)進(jìn)程
-n num Updates to show before exiting. // 刷新次數(shù)
-d num Seconds to wait between updates. // 刷新間隔時(shí)間(默認(rèn)5秒)
-s col Column to sort by <cpu,vss,rss,thr> // 按哪列排序
-t Show threads instead of processes. // 顯示線程信息而不是進(jìn)程
-h Display this help screen. // 顯示幫助文檔
$ top -n 1
top -n 1
就不把執(zhí)行效果放上來了,總之結(jié)果表述如下:
復(fù)制代碼 代碼如下:
User 35%, System 13%, IOW 0%, IRQ 0% // CPU占用率
User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306 // CPU使用情況
PID CPU% S #THR VSS RSS PCY UID Name // 進(jìn)程屬性
xx xx% x xx xx xx xx xx xx
CPU占用率:
User 用戶進(jìn)程
System 系統(tǒng)進(jìn)程
IOW IO等待時(shí)間
IRQ 硬中斷時(shí)間
CPU使用情況(指一個(gè)最小時(shí)間片內(nèi)所占時(shí)間,單位jiffies?;蛘咧杆歼M(jìn)程數(shù)):
User 處于用戶態(tài)的運(yùn)行時(shí)間,不包含優(yōu)先值為負(fù)進(jìn)程
Nice 優(yōu)先值為負(fù)的進(jìn)程所占用的CPU時(shí)間
Sys 處于核心態(tài)的運(yùn)行時(shí)間
Idle 除IO等待時(shí)間以外的其它等待時(shí)間
IOW IO等待時(shí)間
IRQ 硬中斷時(shí)間
SIRQ 軟中斷時(shí)間
進(jìn)程屬性:
PID 進(jìn)程在系統(tǒng)中的ID
CPU% 當(dāng)前瞬時(shí)所以使用CPU占用率
S 進(jìn)程的狀態(tài),其中S表示休眠,R表示正在運(yùn)行,Z表示僵死狀態(tài),N表示該進(jìn)程優(yōu)先值是負(fù)數(shù)。
#THR 程序當(dāng)前所用的線程數(shù)
VSS Virtual Set Size 虛擬耗用內(nèi)存(包含共享庫占用的內(nèi)存)
RSS Resident Set Size 實(shí)際使用物理內(nèi)存(包含共享庫占用的內(nèi)存)
PCY OOXX,不知道什么東東
UID 運(yùn)行當(dāng)前進(jìn)程的用戶id
Name 程序名稱android.process.media
// ps:內(nèi)存占用大小有如下規(guī)律:VSS >= RSS >= PSS >= USS
// PSS Proportional Set Size 實(shí)際使用的物理內(nèi)存(比例分配共享庫占用的內(nèi)存)
// USS Unique Set Size 進(jìn)程獨(dú)自占用的物理內(nèi)存(不包含共享庫占用的內(nèi)存)
在附件Android系統(tǒng)->android top.txt文件內(nèi),自個(gè)總結(jié)的。
2)執(zhí)行代碼
復(fù)制代碼 代碼如下:
// top命令
public static final String[] TOP = { "/system/bin/top", "-n", "1" };
// 現(xiàn)在執(zhí)行top -n 1,我們只需要第二行(用第二行求得CPU占用率,精確數(shù)據(jù))
// 第一行:User 35%, System 13%, IOW 0%, IRQ 0% // CPU占用率
// 第二行:User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306
// // CPU使用情況
public static synchronized String run(String[] cmd) {
String line = "";
InputStream is = null;
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
is = proc.getInputStream();
// 換成BufferedReader
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
do {
line = buf.readLine();
// 前面有幾個(gè)空行
if (line.startsWith("User")) {
// 讀到第一行時(shí),我們?cè)僮x取下一行
line = buf.readLine();
break;
}
} while (true);
if (is != null) {
buf.close();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
// 獲取指定應(yīng)用的top命令獲取的信息
// PID CPU% S #THR VSS RSS PCY UID Name // 進(jìn)程屬性
// 如果當(dāng)前應(yīng)用不在運(yùn)行則返回null
public static synchronized String run(String[] cmd, String pkgName) {
String line = null;
InputStream is = null;
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
is = proc.getInputStream();
// 換成BufferedReader
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
do {
line = buf.readLine();
// 讀取到相應(yīng)pkgName跳出循環(huán)(或者未找到)
if (null == line || line.endsWith(pkgName)) {
break;
}
} while (true);
if (is != null) {
buf.close();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
您可能感興趣的文章:
- Android客制化adb shell進(jìn)去后顯示shell@xxx的標(biāo)識(shí)
- Android 開發(fā)中使用Linux Shell實(shí)例詳解
- Android shell命令行中過濾adb logcat輸出的方法
- Android shell命令行中過濾adb logcat輸出的幾種方法
- Android 使用Shell腳本截屏并自動(dòng)傳到電腦上
- Android中執(zhí)行java命令的方法及java代碼執(zhí)行并解析shell命令
- 實(shí)現(xiàn)android自動(dòng)化測試部署與運(yùn)行Shell腳本分享
- Android系統(tǒng)在shell中的df命令實(shí)現(xiàn)
相關(guān)文章
談?wù)凙ndroid開發(fā)之RecyclerView的使用全解
這篇文章主要介紹了談?wù)凙ndroid開發(fā)之RecyclerView的使用全解,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12android listview 水平滾動(dòng)和垂直滾動(dòng)的小例子
android listview 水平滾動(dòng)和垂直滾動(dòng)的小例子,需要的朋友可以參考一下2013-05-05詳解Flutter WebView與JS互相調(diào)用簡易指南
這篇文章主要介紹了詳解Flutter WebView與JS互相調(diào)用簡易指南,分為JS調(diào)用Flutter和Flutter調(diào)用JS,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04Android入門之Activity四種啟動(dòng)模式(standard、singleTop、singleTask、singl
當(dāng)應(yīng)用運(yùn)行起來后就會(huì)開啟一條線程,線程中會(huì)運(yùn)行一個(gè)任務(wù)棧,當(dāng)Activity實(shí)例創(chuàng)建后就會(huì)放入任務(wù)棧中。Activity啟動(dòng)模式的設(shè)置在AndroidManifest.xml文件中,通過配置Activity的屬性android:launchMode=""設(shè)置2015-12-12Android中SeekBar和RatingBar用法實(shí)例分析
這篇文章主要介紹了Android中SeekBar和RatingBar用法,結(jié)合實(shí)例形式分析了SeekBar和RatingBar的功能、定義與簡單使用方法,需要的朋友可以參考下2016-06-06