在Android手機上, 通過使用adb shell指令可以進入android系統的shell, 該shell除支援一些常用的标準指令之外,還支援一些和android系統相關的其他指令, 這些指令可以列印出系統目前的狀态資訊。 dumpsys就是這樣一個指令。
使用 adb shell 進入指令行, 敲入dumpsys, 列印出的資訊超級多, 在我的nexus 5機器上,輸出多達67000行。 首先從列印資訊的開始部分看起:
Currently running services:
SurfaceFlinger
accessibility
account
activity
alarm
android.security.keystore
appops
appwidget
assetatlas
audio
backup
battery
batterypropreg
batterystats
bluetooth_manager
clipboard
commontime_management
connectivity
consumer_ir
content
country_detector
cpuinfo
dbinfo
device_policy
devicestoragemonitor
diskstats
display
display.qservice
dreams
drm.drmManager
dropbox
entropy
gfxinfo
hardware
input
input_method
iphonesubinfo
isms
location
lock_settings
media.audio_flinger
media.audio_policy
media.camera
media.player
media_router
meminfo
mount
netpolicy
netstats
network_management
nfc
notification
package
permission
phone
power
procstats
samplingprofiler
scheduling_policy
search
sensorservice
serial
servicediscovery
simphonebook
sip
statusbar
telephony.registry
textservices
uimode
updatelock
usagestats
usb
user
vibrator
wallpaper
wifi
wifip2p
window
由此可知,該指令列印出的是系統目前的所有正在運作的服務的資訊。從中可以看到我們常見的一些服務。 activity表示的是ActivityManagerService,用于管理應用程式的四大元件, appops表示的是AppOpsManagerService, 用于管理和配置權限。window表示的是WindowManagerService, 用于管理系統中的視窗, account表示的是AccountManagerService,用于管理系統中的賬戶資訊。
在輸出資訊中, 緊接着的是每個服務目前的詳細狀态資訊,每個服務的狀态資訊一般都比較多, 下面列舉一個資訊較少的服務資訊:
-------------------------------------------------------------------------------
DUMP OF SERVICE accessibility:
ACCESSIBILITY MANAGER (dumpsys accessibility)
User state[attributes:{id=0, currentUser=true, accessibilityEnabled=false, touchExplorationEnabled=false, displayMagnificationEnabled=false}
services:{}]
-------------------------------------------------------------------------------
每個服務資訊之間使用橫線間隔。
在開發過程中, 使用dumpsys的最常用的用途是檢視ActivityManagerService服務的資訊。 下面重點分析ActivityManagerService。 該服務的資訊從下面一行開始:
-------------------------------------------------------------------------------
DUMP OF SERVICE activity:
我們知道android應用程式的四大元件中有三個是由intent來驅動的。是以在ActivityManagerService的資訊中,專門有一部分來記錄目前待處理的Intent, 稱為pending intent 。格式如下:
ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)
* PendingIntentRecord{65ced5f0 com.baidu.BaiduMap broadcastIntent}
uid=10084 packageName=com.baidu.BaiduMap type=broadcastIntent flags=0x0
requestIntent=act=com.baidu.locTest.LocationServer4.1.8
sent=true canceled=false
* PendingIntentRecord{66412b40 com.android.phone broadcastIntent}
uid=1001 packageName=com.android.phone type=broadcastIntent flags=0x0
requestIntent=act=com.android.phone.ACTION_CALL_BACK_FROM_NOTIFICATION dat=tel:153%201571%209213 cmp=com.android.phone/.PhoneGlobals$NotificationBroadcastReceiver
直接在shell中鍵入dumpsys activity intents, 會直接得到這些資訊。 每個待處理的intent在ActivityManagerService内部使用一個PendingIntentRecord對象表示, 并且會顯示出所在應用的包名, 并且會指定該intent是用來發送廣播, 啟動activity或是啟動service。
下面的資訊是系統中的broadcast receiver的狀态資訊, 從下面一行開始:
-------------------------------------------------------------------------------
ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)
直接在shell中鍵入umpsys activity broadcasts, 會直接得到這部分資訊。
首先會列出系統中所有注冊的廣播接收者, 格式如下:
Registered Receivers:
* ReceiverList{660f2e40 857 com.android.systemui/10012/u-1 remote:660f2ca0}
app=857:com.android.systemui/u0a12 pid=857 uid=10012 user=-1
Filter #0: BroadcastFilter{660f2ea0}
Action: "android.intent.action.SHOW_BRIGHTNESS_DIALOG"
* ReceiverList{66045cf0 3867 com.meitu.meiyancamera:pushservice/10091/u0 remote:65e756c8}
app=3867:com.meitu.meiyancamera:pushservice/u0a91 pid=3867 uid=10091 user=0
Filter #0: BroadcastFilter{66045d50}
Action: "android.net.conn.CONNECTIVITY_CHANGE"
之後列出的是位于前台的曆史廣播, 格式如下:
Historical broadcasts [foreground]:
Historical Broadcast foreground #0:
BroadcastRecord{65610478 u-1 android.intent.action.TIME_TICK} to user -1
Intent { act=android.intent.action.TIME_TICK flg=0x50000014 (has extras) }
extras: Bundle[{android.intent.extra.ALARM_COUNT=1}]
caller=android null pid=-1 uid=1000
dispatchClockTime=Tue Oct 07 10:53:00 GMT+08:00 2014
dispatchTime=-14s534ms finishTime=-14s436ms
resultTo=null resultCode=0 resultData=null
resultAbort=false ordered=true sticky=false initialSticky=false
nextReceiver=10 receiver=null
Receiver #0: BroadcastFilter{6568af00 u0 ReceiverList{65692db8 748 system/1000/u0 local:65b2a730}}
Receiver #1: BroadcastFilter{660c8768 u0 ReceiverList{660c81a8 857 com.android.systemui/10012/u0 remote:660c78c0}}
Receiver #2: BroadcastFilter{65d2d230 u0 ReceiverList{65d2cb20 857 com.android.systemui/10012/u0 remote:65a8bb70}}
Receiver #3: BroadcastFilter{65f042e8 u0 ReceiverList{65f04d90 857 com.android.systemui/10012/u0 remote:65f9f2c0}}
Receiver #4: BroadcastFilter{65a9d5f0 u0 ReceiverList{65affbc8 857 com.android.systemui/10012/u0 remote:65b41d50}}
Receiver #5: BroadcastFilter{6682be08 u0 ReceiverList{6682bda8 857 com.android.systemui/10012/u0 remote:665c2478}}
Receiver #6: BroadcastFilter{667a6c48 u0 ReceiverList{6671f128 857 com.android.systemui/10012/u0 remote:662c4048}}
Receiver #7: BroadcastFilter{66901318 u0 ReceiverList{66949870 11260 com.youku.phone:MMS/10080/u0 remote:6695e528}}
Receiver #8: BroadcastFilter{663a4b60 u0 ReceiverList{663a5148 857 com.android.systemui/10012/u0 remote:663c3530}}
Receiver #9: BroadcastFilter{6633df98 u0 ReceiverList{6633f628 857 com.android.systemui/10012/u0 remote:66370860}}
然後是位于背景的曆史廣播,格式如下:
Historical broadcasts [background]:
Historical Broadcast background #0:
BroadcastRecord{66430db0 u0 cn.etouch.ecalendar_CN.ETOUCH.ECALENDAR.WEATHER_HAS_UPDATE} to user 0
Intent { act=cn.etouch.ecalendar_CN.ETOUCH.ECALENDAR.WEATHER_HAS_UPDATE flg=0x10 (has extras) }
extras: Bundle[mParcelledData.dataSize=44]
caller=cn.etouch.ecalendar 3036:cn.etouch.ecalendar:remote/u0a140 pid=3036 uid=10140
dispatchClockTime=Tue Oct 07 10:53:00 GMT+08:00 2014
dispatchTime=-14s475ms finishTime=-14s400ms
resultTo=null resultCode=0 resultData=null
nextReceiver=5 receiver=null
Receiver #0: ResolveInfo{66335e40 cn.etouch.ecalendar/.widget.myWidget_weather p=1000 m=0x108000}
priority=1000 preferredOrder=0 match=0x108000 specificIndex=-1 isDefault=false
ActivityInfo:
name=cn.etouch.ecalendar.widget.myWidget_weather
packageName=cn.etouch.ecalendar
labelRes=0x7f06016c nonLocalizedLabel=null icon=0x0
enabled=true exported=true processName=cn.etouch.ecalendar
taskAffinity=cn.etouch.ecalendar targetActivity=null
ApplicationInfo:
packageName=cn.etouch.ecalendar
labelRes=0x7f060042 nonLocalizedLabel=null icon=0x7f0200c5
className=cn.etouch.ecalendar.common.ApplicationManager
processName=cn.etouch.ecalendar
taskAffinity=cn.etouch.ecalendar
uid=10140 flags=0x88be44 theme=0x7f0a0029
requiresSmallestWidthDp=0 compatibleWidthLimitDp=0 largestWidthLimitDp=0
sourceDir=/data/app/cn.etouch.ecalendar-1.apk
seinfo=default
dataDir=/data/data/cn.etouch.ecalendar
enabled=true targetSdkVersion=11
supportsRtl=false
下面是provider相關的資訊, 格式如下:
-------------------------------------------------------------------------------
ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)
Published single-user content providers (by class):
* ContentProviderRecord{65cd5f60 u0 com.redbend.vdmc/com.redbend.others.NodeProvider}
package=com.redbend.vdmc process=com.redbend.vdmc
proc=ProcessRecord{65d540d8 1018:com.redbend.vdmc/1001}
uid=1001 [email protected]
singleton=true
authority=com.lge.node
* ContentProviderRecord{65d1b150 u0 com.android.phone/.IccProvider}
package=com.android.phone process=com.android.phone
proc=ProcessRecord{65fe2488 1000:com.android.phone/1001}
uid=1001 [email protected]
singleton=true
authority=icc
isSyncable=false multiprocess=true initOrder=0
直接在指令行鍵入dumpsys activity providers,可以直接得到provider相關的資訊。由輸出資訊可知, 在AtivityManagerService中,一個provider元件使用一個ContentProviderRecord對象表示。
服務(Service)相關的資訊如下:
-------------------------------------------------------------------------------
ACTIVITY MANAGER SERVICES (dumpsys activity services)
User 0 active services:
* ServiceRecord{660da0f0 u0 com.android.bluetooth/.hid.HidService}
intent={act=android.bluetooth.IBluetoothInputDevice cmp=com.android.bluetooth/.hid.HidService}
packageName=com.android.bluetooth
processName=com.android.bluetooth
baseDir=/system/app/Bluetooth.apk
dataDir=/data/data/com.android.bluetooth
app=null
createTime=-1d16h27m16s523ms startingBgTimeout=--
lastActivity=-17h20m54s385ms restartTime=-- createdFromFg=false
Bindings:
* IntentBindRecord{660da3e0}:
intent={act=android.bluetooth.IBluetoothInputDevice cmp=com.android.bluetooth/.hid.HidService}
binder=null
requested=false received=false hasBound=false doRebind=false
在shell中直接鍵入dumpsys activity services指令, 可以直接得到Service資訊。
Activity相關的資訊格式如下:
-------------------------------------------------------------------------------
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Stack #0:
Task id #1
* TaskRecord{65cc6860 #1 A=com.google.android.googlequicksearchbox U=0 sz=1}
numActivities=1 rootWasReset=true userId=0 mTaskType=1 numFullscreen=1 mOnTopOfHome=true
affinity=com.google.android.googlequicksearchbox
intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.google.android.googlequicksearchbox/com.google.android.launcher.GEL}
realActivity=com.google.android.googlequicksearchbox/com.google.android.launcher.GEL
Activities=[ActivityRecord{66020ee8 u0 com.google.android.googlequicksearchbox/com.google.android.launcher.GEL t1}]
askedCompatMode=false
las[email protected] lastDescription=null
lastActiveTime=144235207 (inactive for 1424s)
* Hist #0: ActivityRecord{66020ee8 u0 com.google.android.googlequicksearchbox/com.google.android.launcher.GEL t1}
packageName=com.google.android.googlequicksearchbox processName=com.google.android.googlequicksearchbox
launchedFromUid=0 launchedFromPackage=null userId=0
app=ProcessRecord{66105ac8 1058:com.google.android.googlequicksearchbox/u0a19}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.google.android.googlequicksearchbox/com.google.android.launcher.GEL }
frontOfTask=true task=TaskRecord{65cc6860 #1 A=com.google.android.googlequicksearchbox U=0 sz=1}
taskAffinity=com.google.android.googlequicksearchbox
realActivity=com.google.android.googlequicksearchbox/com.google.android.launcher.GEL
baseDir=/system/priv-app/Velvet.apk
dataDir=/data/user/0/com.google.android.googlequicksearchbox
stateNotNeeded=true componentSpecified=false mActivityType=1
compat={480dpi} labelRes=0x7f0a04c5 icon=0x7f030002 theme=0x7f0e000d
config={1.0 460mcc1mnc zh_CN ldltr sw360dp w360dp h567dp 480dpi nrml port finger -keyb/v/h -nav/h s.11}
launchFailed=false launchCount=0 lastLaunchTime=-17h20m56s529ms
haveState=true icicle=Bundle[mParcelledData.dataSize=12720]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=true idle=true
fullscreen=true noDisplay=false immersive=false launchMode=2
frozenBeforeDestroy=false thumbnailNeeded=false forceNewConfig=false
mActivityType=HOME_ACTIVITY_TYPE
thumbHolder: 65cc6860 [email protected] desc=null
waitingVisible=false nowVisible=false lastVisibleTime=-21m53s598ms
Task id #24
* TaskRecord{667de460 #24 A=com.android.systemui U=0 sz=1}
numActivities=1 rootWasReset=false userId=0 mTaskType=2 numFullscreen=1 mOnTopOfHome=true
affinity=com.android.systemui
intent={act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10c00000 cmp=com.android.systemui/.recent.RecentsActivity}
realActivity=com.android.systemui/.recent.RecentsActivity
Activities=[ActivityRecord{6561e970 u0 com.android.systemui/.recent.RecentsActivity t24}]
askedCompatMode=false
las[email protected] lastDescription=null
lastActiveTime=87755110 (inactive for 57904s)
* Hist #0: ActivityRecord{6561e970 u0 com.android.systemui/.recent.RecentsActivity t24}
packageName=com.android.systemui processName=com.android.systemui
launchedFromUid=10012 launchedFromPackage=com.android.systemui userId=0
app=ProcessRecord{6602d698 857:com.android.systemui/u0a12}
Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS flg=0x10800000 cmp=com.android.systemui/.recent.RecentsActivity }
frontOfTask=true task=TaskRecord{667de460 #24 A=com.android.systemui U=0 sz=1}
taskAffinity=com.android.systemui
realActivity=com.android.systemui/.recent.RecentsActivity
baseDir=/system/priv-app/SystemUI.apk
dataDir=/data/data/com.android.systemui
stateNotNeeded=false componentSpecified=true mActivityType=2
compat={480dpi} labelRes=0x7f0a0080 icon=0x10804a5 theme=0x7f0e0000
config={1.0 460mcc1mnc zh_CN ldltr sw360dp w360dp h567dp 480dpi nrml port finger -keyb/v/h -nav/h s.9}
launchFailed=false launchCount=0 lastLaunchTime=-9h59m21s658ms
haveState=true icicle=Bundle[mParcelledData.dataSize=44]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=true idle=true
fullscreen=true noDisplay=false immersive=false launchMode=3
frozenBeforeDestroy=false thumbnailNeeded=false forceNewConfig=false
mActivityType=RECENTS_ACTIVITY_TYPE
thumbHolder: 667de460 [email protected] desc=null
waitingVisible=false nowVisible=false lastVisibleTime=-7h8m31s600ms
Running activities (most recent first):
TaskRecord{65cc6860 #1 A=com.google.android.googlequicksearchbox U=0 sz=1}
Run #1: ActivityRecord{66020ee8 u0 com.google.android.googlequicksearchbox/com.google.android.launcher.GEL t1}
TaskRecord{667de460 #24 A=com.android.systemui U=0 sz=1}
Run #0: ActivityRecord{6561e970 u0 com.android.systemui/.recent.RecentsActivity t24}
mLastPausedActivity: ActivityRecord{66020ee8 u0 com.google.android.googlequicksearchbox/com.google.android.launcher.GEL t1}
在shell中直接鍵入dumpsys activity activities, 可以直接得到activity的相關資訊。
和其他元件相比, Activity的管理比較特殊。 Activity是以棧的形式管理的, 每個棧中存在若幹個任務(task), 每個任務又由若幹個Activity組成。
上面簡單介紹了ActivityManagerService中的四大元件相關的資訊。 關于其他服務中的資訊, 由于資訊量太大, 無法一一介紹, 請讀者自行檢視。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支援!