Android執行shell命令

2021-09-21 08:40:24 字數 3584 閱讀 4441

android執行shell命令

一、方法

/**

* 執行乙個shell命令,並返回字串值

** @param cmd

* 命令名稱&引數組成的陣列(例如:)

* @param workdirectory

* 命令執行路徑(例如:"system/bin/")

* @return 執行結果組成的字串

* @throws ioexception

*/public

static

synchronized

string run(string cmd, string workdirectory) 

throws

ioexception  

} // 關閉輸入流

if(in != 

null

)  } catch

(exception ex)  

return

result.tostring(); 

}二、用途

執行linux下的top、ps等命令,這些命令你也通過adb可以執行檢視效果。

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 

todisplay. // 最多顯示多少個程序 

-n num  updates to

show before exiting. // 重新整理次數 

-d num  seconds to

wait 

between

updates. // 重新整理間隔時間(預設5秒) 

-s col  column

tosort 

by// 按哪列排序 

-t      show threads instead

ofprocesses. // 顯示執行緒資訊而不是程序 

-h      display this help screen. // 顯示幫助文件 

$ top

-n 1 

top-n 1 

就不把執行效果放上來了,總之結果表述如下:

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

// 程序屬性 

xx  xx% x   xx  xx  xx  xx  xx   xx 

cpu佔用率: 

user

使用者程序 

system  系統程序 

iow io等待時間 

irq 硬中斷時間 

cpu使用情況(指乙個最小時間片內所佔時間,單位jiffies。或者指所佔程序數): 

user

處於使用者態的執行時間,不包含優先值為負程序 

nice    優先值為負的程序所占用的cpu時間 

sys 處於核心態的執行時間 

idle    除io等待時間以外的其它等待時間 

iow io等待時間 

irq 硬中斷時間 

sirq    軟中斷時間 

程序屬性: 

pid 程序在系統中的id 

cpu%    當前瞬時所以使用cpu佔用率 

s   程序的狀態,其中s表示休眠,r表示正在執行,z表示僵死狀態,n表示該程序優先值是負數。 

#thr    程式當前所用的執行緒數 

vss virtual set

size

虛擬耗用記憶體(包含共享庫占用的記憶體) 

rss resident set

size

實際使用物理記憶體(包含共享庫占用的記憶體) 

pcy ooxx,不知道什麼東東 

uid 執行當前程序的使用者id 

name

程式名稱android.process.media 

// ps:記憶體占用大小有如下規律:vss >= rss >= pss >= uss 

// pss  proportional set

size

實際使用的物理記憶體(比例分配共享庫占用的記憶體) 

// uss  unique

setsize

程序獨自占用的物理記憶體(不包含共享庫占用的記憶體) 

在附件android系統->android top.txt檔案內,自個總結的。

2)執行**

// top命令

public

static

final

string top = ; 

// 現在執行top -n 1,我們只需要第二行(用第二行求得cpu佔用率,精確資料)

// 第一行: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)  

} while

(true

); if

(is != 

null

)  } catch

(ioexception e)  

return

line; 

} // 獲取指定應用的top命令獲取的資訊

// pid cpu% s #thr vss rss pcy uid name // 程序屬性

// 如果當前應用不在執行則返回null

public

static

synchronized

string run(string cmd, string pkgname)  

} while

(true

); if

(is != 

null

)  } catch

(ioexception e)  

return

line; 

}三、後記

這次相關的僅有的那個工程好像不能放上來了==。

資料夾內容如下:

ps:現在有個可以看的小工程了,請移至《

android vnc server new

》!附件:

Android執行shell命令

android執行shell命令 一 方法 執行乙個shell命令,並返回字串值 param cmd 命令名稱 引數組成的陣列 例如 param workdirectory 命令執行路徑 例如 system bin return 執行結果組成的字串 throws ioexception public...

Android執行shell命令 top ps

android執行shell命令 一 方法 執行乙個shell命令,並返回字串值 param cmd 命令名稱 引數組成的陣列 例如 param workdirectory 命令執行路徑 例如 system bin return 執行結果組成的字串 throws ioexception public...

Android開機執行shell指令碼

往往需要在android啟動過程中去對系統做一些設定,如修改某些資料夾的屬主和檔案的許可權,可在init.rc中增加service執行shell指令碼,從而可在shell指令碼中完成大部分操作。1 在system bin 下建立乙個shell指令碼init.mbx.sh,以修改mtd許可權為例,內容...