阅读
http://developer.android.com/reference/android/app/Service.html
和
http://developer.android.com/guide/components/services.html我读过两条重要声明:
1)系统可以运行服务有两个原因。如果有人调用Context.startService(),那么系统将检索服务(创建它并在需要时调用它的onCreate()方法)然后调用它的onStartCommand(Intent,int,int)方法。客户提供的参数。 此服务将在此时继续运行,直到调用Context.stopService()或stopSelf()。
2)已启动的服务可以使用startForeground(int,Notification)API将服务置于前台状态,其中系统认为它是用户主动了解的内容,因此在低时不是候选者在内存上。(理论上,在当前前台应用程序的极端内存压力下,服务仍然可能会被杀死,但实际上这不应该是一个问题。)
我的目标是创建一个在单独进程中运行的远程服务(在Manifest上声明android:process)并确保该进程始终处于活动状态,除非Android S.O.回收记忆。
有哪些禁忌症以及这3种不同实施方式之间的区别是什么?
从onBind()调用startService():
public class ExampleService extends Service {
...
@Override
public IBinder onBind(Intent intent) {
startService(new Intent(this, ExampleService.class));
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
}
...
}
从onCreate()调用startForegorund():
public class ExampleService extends Service {
...
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
...
startForeground(ord, note);
}
...
}
从onCreate()调用startForegorund(),从onBind()调用startService:
public class ExampleService extends Service {
...
@Override
public IBinder onBind(Intent intent) {
startService(new Intent(this, ExampleService.class));
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
...
startForeground(ord, note);
}
...
}
客户端将使用此服务调用bindService()并使用IBinder。
答案 0 :(得分:0)
我有关于这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)svc
2)prcp
3)bak