4

阅读 http://developer.android.com/reference/android/app/Service.htmlhttp://developer.android.com/guide/components/services.html我已经阅读了两个重要的声明:

1)有两个服务可以由系统运行的原因。如果有人调用 Context.startService(),那么系统将检索服务(创建它并在需要时调用其 onCreate() 方法),然后使用客户端提供的参数调用其 onStartCommand(Intent, int, int) 方法。此时服务将继续运行,直到调用 Context.stopService() 或 stopSelf()。

2) 启动的服务可以使用 startForeground(int, Notification) API 将服务置于前台状态,其中系统认为它是用户主动意识到的,因此在内存不足时不适合杀死。(理论上,在当前前台应用程序的极端内存压力下,服务仍然有可能被杀死,但实际上这不应该是一个问题。)

我的目标是创建一个在单独进程中运行的远程服务(声明 android:process在 Manifest 上)并确保进程始终处于活动状态,除非 Android SO 回收内存。

有什么禁忌症,这三种不同的实现方式有什么区别?

从 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。

4

1 回答 1

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) 服务端

2) prcp

3) 烤肉

于 2013-05-26T15:00:57.167 回答