我有关于这 3 个用例的更多信息,我已经尝试了这三个用例,并且在启动应用程序、绑定服务并将活动置于后台(按主页按钮)后,我对每一个用例都进行了“dumpsys 活动进程”:
第一种情况(仅在 onBind() 中调用 startService()):
*APP* UID 10137 ProcessRecord{40b01d30 6719:com.mypackage:service_process/10137}
class=com.mypackage.app.MyApp
dir=/data/app/com.mypackage-1.apk publicDir=/data/app/com.mypackage-1.apk data=/data/data/com.mypackage
packageList=[com.mypackage]
thread=android.app.ApplicationThreadProxy@40d1b540 curReceiver=null
pid=6719 starting=false lastPss=0
lastActivityTime=-1m42s491ms lruWeight=671144 keeping=true hidden=false empty=true
oom: max=15 hidden=8 curRaw=4 setRaw=4 cur=4 set=4
curSchedGroup=1 setSchedGroup=1
setIsForeground=false foregroundServices=false forcingToForeground=null
persistent=false removed=false
adjSeq=5157 lruSeq=1017
lastRequestedGc=-1m46s82ms lastLowMemory=-1m46s82ms reportLowMemory=false
services=[ServiceRecord{40a68aa0 com.mypackage/.model.page.ServerConnection}]
connections=[ConnectionRecord{40d11118 com.mypackage/.util.CommonService:@40d12928}, ConnectionRecord{40bca008 com.mypackage/.model.page.ServerConnection:@40d0bd08}, ConnectionRecord{40d64030 com.mypackage/.util.log.LoggerService:@40d34b38}]
Running processes (most recent first):
Proc #16: adj=svc /B 40b01d30 6719:com.mypackage:service_process/10137 (started-services)
第二种情况(仅在 onCreate() 中调用 startForeground()):
*APP* UID 10137 ProcessRecord{40c47420 7921:com.mypackage:service_process/10137}
class=com.mypackage.app.MyApp
dir=/data/app/com.mypackage-2.apk publicDir=/data/app/com.mypackage-2.apk data=/data/data/com.mypackage
packageList=[com.mypackage]
thread=android.app.ApplicationThreadProxy@40a044c8 curReceiver=null
pid=7921 starting=false lastPss=0
lastActivityTime=-27s701ms lruWeight=1232643 keeping=true hidden=false empty=false
oom: max=15 hidden=8 curRaw=2 setRaw=2 cur=2 set=2
curSchedGroup=0 setSchedGroup=0
setIsForeground=false foregroundServices=true forcingToForeground=null
persistent=false removed=false
adjSeq=6068 lruSeq=1170
lastRequestedGc=-32s123ms lastLowMemory=-32s123ms reportLowMemory=false
services=[ServiceRecord{40b3b1d0 com.mypackage/.model.page.ServerConnection}]
connections=[ConnectionRecord{40b18da8 com.mypackage/.util.log.LoggerService:@40b18b88}, ConnectionRecord{40c86fa0 com.mypackage/.model.page.ServerConnection:@40c86d80}, ConnectionRecord{40c869a0 com.mypackage/.util.CommonService:@409626c8}]
Running processes (most recent first):
Proc #18: adj=prcp /F 40c47420 7921:com.mypackage:service_process/10137 (foreground-service)
第三种情况(调用 onBind() 中的 startService() 和 onCreate() 中的 startForeground()):
*APP* UID 10137 ProcessRecord{40d35d30 8407:com.mypackage:service_process/10137}
class=com.mypackage.app.MyApp
dir=/data/app/com.mypackage-1.apk publicDir=/data/app/com.mypackage-1.apk data=/data/data/com.mypackage
packageList=[com.mypackage]
thread=android.app.ApplicationThreadProxy@40a6f7a0 curReceiver=null
pid=8407 starting=false lastPss=0
lastActivityTime=-6s98ms lruWeight=1423485 keeping=true hidden=false empty=false
oom: max=15 hidden=7 curRaw=2 setRaw=2 cur=2 set=2
curSchedGroup=0 setSchedGroup=0
setIsForeground=false foregroundServices=true forcingToForeground=null
persistent=false removed=false
adjSeq=6565 lruSeq=1289
lastRequestedGc=-10s334ms lastLowMemory=-10s334ms reportLowMemory=false
services=[ServiceRecord{40ba7650 com.mypackage/.model.page.ServerConnection}]
connections=[ConnectionRecord{40ac5408 com.mypackage/.model.page.ServerConnection:@40ac5228}, ConnectionRecord{40c760e8 com.mypackage/.util.CommonService:@40c31fa8}, ConnectionRecord{40c31c00 com.mypackage/.util.log.LoggerService:@40bc4bf0}]
Running processes (most recent first):
Proc #13: adj=prcp /F 40d35d30 8407:com.mypackage:service_process/10137 (foreground-service)
默认情况(不调用startService()和startForeground(),只调用绑定服务):
*APP* UID 10137 ProcessRecord{40c3eb78 5609:com.mypackage:service_process/10137}
class=com.mypackage.app.MyApp
dir=/data/app/com.mypackage-2.apk publicDir=/data/app/com.mypackage-2.apk data=/data/data/com.mypackage
packageList=[com.mypackage]
thread=android.app.ApplicationThreadProxy@40a34c90 curReceiver=null
pid=5609 starting=false lastPss=0
lastActivityTime=-5m31s175ms lruWeight=227344 keeping=false hidden=true empty=true
oom: max=15 hidden=7 curRaw=7 setRaw=7 cur=7 set=7
curSchedGroup=1 setSchedGroup=1
setIsForeground=false foregroundServices=false forcingToForeground=null
persistent=false removed=false
adjSeq=4483 lruSeq=887
lastWakeTime=0 time used=0
lastCpuTime=0 time used=0
lastRequestedGc=-2m1s649ms lastLowMemory=-6m7s191ms reportLowMemory=false
services=[ServiceRecord{40d559b8 com.mypackage/.model.page.ServerConnection}]
connections=[ConnectionRecord{40beeef0 com.mypackage/.model.page.ServerConnection:@40bd4620}, ConnectionRecord{40bc4c60 com.mypackage/.util.CommonService:@40b69ea0}, ConnectionRecord{40ac74b0 com.mypackage/.util.log.LoggerService:@40ab5d60}]
Running processes (most recent first):
Proc #22: adj=bak+3/B 40c3eb78 5609:com.mypackage:service_process/10137 (bg-empty)
我认为最重要的是adj 的值在所有情况下都会发生变化并假设这些值:
1) 服务端
2) prcp
3) 烤肉