1. 程式人生 > >Monkey詳解(事件型指令篇)

Monkey詳解(事件型指令篇)

(七)事件型指令
事件型別指令可實現四個主要功能。

(1)復現隨機事件

Monkey最大的特點就是偽隨機。之所以加個“偽”字,是因為該隨機事件可進行復現。
我們每次執行一組隨機事件,系統都會給一個隨機的ID,稱之為seed。例如

C:\Users\XXXX>adb shell monkey -p com.breakloop.butterknifedemo -v 100
:Monkey: seed=1503779100077 count=100
:AllowPackage: com.breakloop.butterknifedemo
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory:
android.intent.category.MONKEY // Event percentages: .... ## Network stats: elapsed time=83ms (0ms mobile, 0ms wifi, 83ms not connected) // Monkey finished C:\Users\XXXX>

若我們想將該組隨機事件再次執行一遍,則使用-s+seed+count即可。

C:\Users\yisong>adb shell monkey -v -s 1503779100077 100
:Monkey: seed=1503779100077 count=100
:IncludeCategory:
android.intent.category.LAUNCHER :IncludeCategory: android.intent.category.MONKEY // Event percentages: ... ## Network stats: elapsed time=588ms (0ms mobile, 0ms wifi, 588ms not connected) // Monkey finished C:\Users\yisong>

除了執行事件不同,其他都與之前一樣。

那麼問題來了
(a)如果seed後的count,與之前不同,執行結果會如何?
若count變小,則從前往後,執行count個事件。
若count變大,則在執行完後,補充執行差數個隨機事件。
(b)seed何時過期?
重新連線裝置,kill-server&start-server,重啟手機,都不會對seed有影響。

(2)指定事件間隔

在相鄰的兩條隨機事件之間,預設沒有事件間隔。因此,有時會引起隨機事件的注入失敗。

:Sending Touch (ACTION_DOWN): 0:(736.0,455.0)
    // Injection Failed
:Sending Touch (ACTION_UP): 0:(736.2039,467.69113)
    // Injection Failed
:Sending Flip keyboardOpen=false
Got IOException performing flipjava.io.IOException: write failed: EINVAL (Invali
d argument)
    // Injection Failed
:Sending Touch (ACTION_DOWN): 0:(352.0,1698.0)
    // Injection Failed
:Sending Touch (ACTION_UP): 0:(349.23996,1686.784)
    // Injection Failed
:Sending Trackball (ACTION_MOVE): 0:(-5.0,-4.0)
:Sending Trackball (ACTION_UP): 0:(0.0,0.0)
:Sending Touch (ACTION_DOWN): 0:(788.0,1048.0)
    // Injection Failed
:Sending Touch (ACTION_UP): 0:(780.9308,1044.052)
    // Injection Failed
:Sending Trackball (ACTION_MOVE): 0:(0.0,-2.0)

為了避免這類情況,須在事件間新增“等待”。使用引數–throttle+時間間隔。
throttle(油門)?應該命名為剎車吧。
需要說明的是
(a)引數前是兩個“-”
(b)時間單位為毫秒

:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.vmall.client/.activity.VmallWa
pActivity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.vmall.client/.activity.VmallWapActivity } in pac
kage com.vmall.client
Sleeping for 1000 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(-5.0,-5.0)
:Sending Trackball (ACTION_MOVE): 0:(0.0,1.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,-5.0)
:Sending Trackball (ACTION_MOVE): 0:(-5.0,-4.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,-1.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,1.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,4.0)
:Sending Trackball (ACTION_MOVE): 0:(-4.0,4.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,-2.0)
:Sending Trackball (ACTION_MOVE): 0:(3.0,-3.0)
:Sending Trackball (ACTION_DOWN): 0:(0.0,0.0)
:Sending Trackball (ACTION_UP): 0:(0.0,0.0)
Sleeping for 1000 milliseconds
    // Allowing start of Intent { cmp=com.vmall.client/.activity.PolicyWebActivi
ty } in package com.vmall.client
:Sending Key (ACTION_DOWN): 82    // KEYCODE_MENU
:Sending Key (ACTION_UP): 82    // KEYCODE_MENU
Sleeping for 1000 milliseconds
:Sending Touch (ACTION_DOWN): 0:(139.0,398.0)
:Sending Touch (ACTION_MOVE): 0:(157.42459,406.12418)
:Sending Touch (ACTION_MOVE): 0:(175.66505,407.0996)
:Sending Touch (ACTION_MOVE): 0:(185.49532,419.4165)
:Sending Touch (ACTION_MOVE): 0:(199.77296,435.93414)
:Sending Touch (ACTION_MOVE): 0:(215.76842,453.50436)
:Sending Touch (ACTION_MOVE): 0:(232.69334,470.774)
:Sending Touch (ACTION_MOVE): 0:(254.05827,476.62515)
:Sending Touch (ACTION_MOVE): 0:(262.36322,481.53244)
:Sending Touch (ACTION_UP): 0:(281.79745,489.0132)
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 82    // KEYCODE_MENU

注:從log來看,並不是所有的事件後都有“等待”。個人感覺,是在事件無法第一時間得到響應時,才去等待。

(3)指定某類事件所佔比例

那麼我們都有哪些事件呢?
各個事件都代表什麼操作?
如果不設定事件比例,有沒有預設的比例?
我們來一一解釋。

先來看一組隨機事件LOG。

C:\Users\XXXX>adb shell monkey --throttle 1000 -v 10
:Monkey: seed=1503647257950 count=10
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Event percentages:
//   0: 15.0%
//   1: 10.0%
//   2: 2.0%
//   3: 15.0%
//   4: -0.0%
//   5: -0.0%
//   6: 25.0%
//   7: 15.0%
//   8: 2.0%
//   9: 2.0%
//   10: 1.0%
//   11: 13.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.huawei.android.findmyphone/.ui
.FindMyPhoneActivity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.huawei.android.findmyphone/.ui.FindMyPhoneActivi
ty } in package com.huawei.android.findmyphone
:Sending Touch (ACTION_DOWN): 0:(236.0,163.0)
:Sending Touch (ACTION_UP): 0:(247.74335,163.03857)
:Sending Touch (ACTION_DOWN): 0:(819.0,1411.0)
:Sending Touch (ACTION_UP): 0:(825.0237,1402.9965)
:Sending Touch (ACTION_DOWN): 0:(745.0,1572.0)
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=5113ms (0ms mobile, 0ms wifi, 5113ms not connecte
d)
// Monkey finished

其中有一個Event percentages。可見,如果不使用–pct-XXXX指令,則按照預設比例觸發隨機事件。只是,不知道索引對應的事件是什麼。

按照引數,可以分為八類。
(a)–pct-touch:觸控事件。即在某一位置的Down-Up(手指的放下和擡起)事件。Down(ACTION_DOWN)和Up(ACTION_UP)的座標臨近,但並非相同。

C:\Users\XXXX>adb shell monkey --throttle 1000 -v --pct-touch 100 10
:Monkey: seed=1503671202592 count=10
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Event percentages:
//   0: 100.0%
//   1: 0.0%
//   2: 0.0%
//   3: 0.0%
//   4: -0.0%
//   5: -0.0%
//   6: 0.0%
//   7: 0.0%
//   8: 0.0%
//   9: 0.0%
//   10: 0.0%
//   11: 0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.gemalto.qrcodelib/.MainActivit
y;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.gemalto.qrcodelib/.MainActivity } in package com
.gemalto.qrcodelib
:Sending Touch (ACTION_DOWN): 0:(186.0,781.0)
:Sending Touch (ACTION_UP): 0:(181.72902,780.3934)
:Sending Touch (ACTION_DOWN): 0:(600.0,384.0)
:Sending Touch (ACTION_UP): 0:(609.29193,385.8893)
:Sending Touch (ACTION_DOWN): 0:(786.0,1742.0)
:Sending Touch (ACTION_UP): 0:(782.567,1737.6458)
:Sending Touch (ACTION_DOWN): 0:(733.0,1538.0)
:Sending Touch (ACTION_UP): 0:(742.7612,1545.8889)
:Sending Touch (ACTION_DOWN): 0:(176.0,1538.0)
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=5093ms (0ms mobile, 0ms wifi, 5093ms not connecte
d)
// Monkey finished

(b)–pct-motion:動作事件。以Down(ACTION_DOWN)開始,Up(ACTION_UP)結尾,中間至少有一次Move(ACTION_MOVE)。

C:\Users\XXXX>adb shell monkey --throttle 1000 -v -v --pct-motion 100 10
:Monkey: seed=1503775969786 count=10
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Seeded: 1503775969786
// Event percentages:
//   0: 0.0%
//   1: 100.0%
//   2: 0.0%
//   3: 0.0%
//   4: -0.0%
//   5: -0.0%
//   6: 0.0%
//   7: 0.0%
//   8: 0.0%
//   9: 0.0%
//   10: 0.0%
//   11: 0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.huawei.android.remotecontrolle
r/.StartActivity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.huawei.android.remotecontroller/.StartActivity }
 in package com.huawei.android.remotecontroller
Sleeping for 1000 milliseconds
    // Allowing start of Intent { cmp=com.huawei.android.remotecontroller/.app.W
elcomeActivity } in package com.huawei.android.remotecontroller
    // activityResuming(com.huawei.android.remotecontroller)
:Sending Touch (ACTION_DOWN): 0:(241.0,1334.0)
:Sending Touch (ACTION_MOVE): 0:(245.10721,1333.8828)
:Sending Touch (ACTION_MOVE): 0:(246.83798,1331.2542)
:Sending Touch (ACTION_MOVE): 0:(248.75221,1324.734)
:Sending Touch (ACTION_MOVE): 0:(249.26602,1323.9482)
:Sending Touch (ACTION_MOVE): 0:(251.57216,1317.7059)
:Sending Touch (ACTION_MOVE): 0:(255.50874,1314.2244)
:Sending Touch (ACTION_UP): 0:(259.62653,1307.603)
Sleeping for 1000 milliseconds
:Sending Touch (ACTION_DOWN): 0:(1031.0,1108.0)
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=2060ms (0ms mobile, 0ms wifi, 2060ms not connecte
d)
// Monkey finished

(c)–pct-trackball:軌跡球事件。即單純的Move(ACTION_MOVE)。

C:\Users\XXXX>adb shell monkey --throttle 1000 -v -v --pct-trackball 100 10
:Monkey: seed=1503665474331 count=10
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Seeded: 1503665474331
// Event percentages:
//   0: 0.0%
//   1: 0.0%
//   2: 0.0%
//   3: 100.0%
//   4: -0.0%
//   5: -0.0%
//   6: 0.0%
//   7: 0.0%
//   8: 0.0%
//   9: 0.0%
//   10: 0.0%
//   11: 0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.yunos.cloudkit.demo/.MainActiv
ity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.yunos.cloudkit.demo/.MainActivity } in package c
om.yunos.cloudkit.demo
Sleeping for 1000 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(-5.0,-3.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,1.0)
:Sending Trackball (ACTION_MOVE): 0:(-1.0,3.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,-1.0)
:Sending Trackball (ACTION_MOVE): 0:(-4.0,3.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,-3.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,-4.0)
:Sending Trackball (ACTION_MOVE): 0:(-4.0,-3.0)
:Sending Trackball (ACTION_MOVE): 0:(-2.0,-3.0)
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=1045ms (0ms mobile, 0ms wifi, 1045ms not connecte
d)
// Monkey finished

(d)–pct-nav:基本導航事件。即來自於方向輸入裝置的上下左右操作。

C:\Users\XXXX>adb shell monkey --throttle 1000 -v -v --pct-nav 100 10
:Monkey: seed=1503699935813 count=10
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Seeded: 1503699935813
// Event percentages:
//   0: 0.0%
//   1: 0.0%
//   2: 0.0%
//   3: 0.0%
//   4: -0.0%
//   5: -0.0%
//   6: 100.0%
//   7: 0.0%
//   8: 0.0%
//   9: 0.0%
//   10: 0.0%
//   11: 0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.gto.paltesttoolui/com.gto.tsm.
instrumentation.EmmaInstrumentation%24InstrumentedActivity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.gto.paltesttoolui/com.gto.tsm.instrumentation.Em
maInstrumentation$InstrumentedActivity } in package com.gto.paltesttoolui
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 22    // KEYCODE_DPAD_RIGHT
:Sending Key (ACTION_UP): 22    // KEYCODE_DPAD_RIGHT
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 22    // KEYCODE_DPAD_RIGHT
:Sending Key (ACTION_UP): 22    // KEYCODE_DPAD_RIGHT
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 20    // KEYCODE_DPAD_DOWN
:Sending Key (ACTION_UP): 20    // KEYCODE_DPAD_DOWN
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 21    // KEYCODE_DPAD_LEFT
:Sending Key (ACTION_UP): 21    // KEYCODE_DPAD_LEFT
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 21    // KEYCODE_DPAD_LEFT
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=5116ms (0ms mobile, 0ms wifi, 5116ms not connecte
d)
// Monkey finished

(e)–pct-majornav:主導航事件。即Navigation Bar的確認,選單,返回鍵等。

C:\Users\XXXX>adb shell monkey --throttle 1000 -v -v --pct-majornav 100 10
:Monkey: seed=1503700822988 count=10
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Seeded: 1503700822988
// Event percentages:
//   0: 0.0%
//   1: 0.0%
//   2: 0.0%
//   3: 0.0%
//   4: -0.0%
//   5: -0.0%
//   6: 0.0%
//   7: 100.0%
//   8: 0.0%
//   9: 0.0%
//   10: 0.0%
//   11: 0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.android.settings/.HWSettings;e
nd
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.android.settings/.HWSettings } in package com.an
droid.settings
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 82    // KEYCODE_MENU
:Sending Key (ACTION_UP): 82    // KEYCODE_MENU
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 82    // KEYCODE_MENU
:Sending Key (ACTION_UP): 82    // KEYCODE_MENU
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 23    // KEYCODE_DPAD_CENTER
:Sending Key (ACTION_UP): 23    // KEYCODE_DPAD_CENTER
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 82    // KEYCODE_MENU
:Sending Key (ACTION_UP): 82    // KEYCODE_MENU
Sleeping for 1000 milliseconds
:Sending Key (ACTION_DOWN): 23    // KEYCODE_DPAD_CENTER
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=5100ms (0ms mobile, 0ms wifi, 5100ms not connecte
d)
// Monkey finished

(f)–pct-syskeys:系統按鍵事件。即系統保留按鍵,如HOME鍵,BACK鍵,撥號鍵,結束通話鍵,音量鍵等。

C:\Users\XXXX>adb shell monkey -v -v --pct-syskeys 100 10
:Monkey: seed=1503762438503 count=10
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Seeded: 1503762438503
// Event percentages:
//   0: 0.0%
//   1: 0.0%
//   2: 0.0%
//   3: 0.0%
//   4: -0.0%
//   5: -0.0%
//   6: 0.0%
//   7: 0.0%
//   8: 100.0%
//   9: 0.0%
//   10: 0.0%
//   11: 0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.huawei.phoneservice/.ui.HelpCe
nterActivity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.huawei.phoneservice/.ui.HelpCenterActivity } in
package com.huawei.phoneservice
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 24    // KEYCODE_VOLUME_UP
    // Allowing start of Intent { cmp=com.huawei.phoneservice/com.huawei.phonese
rviceuni.start.UserAgreementActivity } in package com.huawei.phoneservice
:Sending Key (ACTION_UP): 24    // KEYCODE_VOLUME_UP
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 25    // KEYCODE_VOLUME_DOWN
:Sending Key (ACTION_UP): 25    // KEYCODE_VOLUME_DOWN
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 25    // KEYCODE_VOLUME_DOWN
:Sending Key (ACTION_UP): 25    // KEYCODE_VOLUME_DOWN
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 24    // KEYCODE_VOLUME_UP
:Sending Key (ACTION_UP): 24    // KEYCODE_VOLUME_UP
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 24    // KEYCODE_VOLUME_UP
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=765ms (0ms mobile, 0ms wifi, 765ms not connected)

// Monkey finished

(g)–pct-appswitch:應用啟動事件

C:\Users\XXXX>adb shell monkey -v --pct-appswitch 100 3
:Monkey: seed=1503724767031 count=3
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Event percentages:
//   0: 0.0%
//   1: 0.0%
//   2: 0.0%
//   3: 0.0%
//   4: -0.0%
//   5: -0.0%
//   6: 0.0%
//   7: 0.0%
//   8: 0.0%
//   9: 100.0%
//   10: 0.0%
//   11: 0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.cate
ory.LAUNCHER;launchFlags=0x10200000;component=com.huawei.fans/.activity.MainAct
vity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.i
tent.category.LAUNCHER] cmp=com.huawei.fans/.activity.MainActivity } in package
com.huawei.fans
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.cate
ory.LAUNCHER;launchFlags=0x10200000;component=com.huawei.appmarket/.MainActivit
;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.i
tent.category.LAUNCHER] cmp=com.huawei.appmarket/.MainActivity } in package com
huawei.appmarket
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.cate
ory.LAUNCHER;launchFlags=0x10200000;component=com.android.soundrecorder/.SoundR
corder;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.i
tent.category.LAUNCHER] cmp=com.android.soundrecorder/.SoundRecorder } in packa
e com.android.soundrecorder
Events injected: 3
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=75ms (0ms mobile, 0ms wifi, 75ms not connected)
// Monkey finished

(h)–pct-anyevent:其他未提及事件。該事件可能包含其他上述事件。

C:\Users\XXXX>adb shell monkey -v -v --pct-anyevent 100 10
:Monkey: seed=1503761055148 count=10
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Seeded: 1503761055148
// Event percentages:
//   0: 0.0%
//   1: 0.0%
//   2: 0.0%
//   3: 0.0%
//   4: -0.0%
//   5: -0.0%
//   6: 0.0%
//   7: 0.0%
//   8: 0.0%
//   9: 0.0%
//   10: 0.0%
//   11: 100.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.huawei.health/.MainActivity;en
d
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.huawei.health/.MainActivity } in package com.hua
wei.health
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 549    // 549
:Sending Key (ACTION_UP): 549    // 549
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 507    // 507
:Sending Key (ACTION_UP): 507    // 507
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 362    // 362
:Sending Key (ACTION_UP): 362    // 362
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 673    // 673
:Sending Key (ACTION_UP): 673    // 673
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 25    // KEYCODE_VOLUME_DOWN
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=691ms (0ms mobile, 0ms wifi, 691ms not connected)

// Monkey finished

仔細觀察各個事件的Log,不難發現:
(I)所有隨機事件,若count>0,第一個事件永遠是appswitch。必須要有一個activity作為操作物件!!
(II)將某種事件所佔百分比,調至100,可得出事件索引對應關係。
0–> –pct-touch
1–> –pct-motion
3–> –pct-trackball
6–> –pct-nav
7–> –pct-majornav
8–> –pct-syskeys
9–> –pct-appswitch
11–> –pct-anyevent

(III)若使用一個-v,ACTION_MOVE事件或KEYCODE_DPAD_XXXX不會顯示在LOG中。

(4)執行指令碼

使用引數-f來指定指令碼path。

adb shell monkey -f c:\d\test 100

檔案可用沒有後綴名。
注:此時count=100,是指將該指令碼執行100次。

對於指令碼的編寫,我們將用單獨的篇幅來講解。