For a long time for the Android service: Immortal Service

Reading http://developer.android.com/reference/android/app/Service.html and also http://developer.android.com/guide/components/services.html I read two important statements:

1) There are two reasons why a system can manage a service. If someone calls Context.startService () , then the system will retrieve the service (by creating it and calling its onCreate () method, if necessary), and then calling its onStartCommand (Intent, int, int) method using the arguments provided by customer. The service will continue to run until the call to Context.stopService () or stopSelf ().

2) The initial service can use the startForeground (int, Notification) API to put the service in the foreground state, where the system believes that this is something that the user is actively aware of and therefore not a candidate for killing with a low memory level. (It is theoretically possible for a service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a problem.)

My goal is to create a remote service running in a separate process (declaring android: the process on the manifest) and make sure that the process is always active if only Android SO returns the memory.

What contraindications exist and what are the differences between these three different realizations?

Call startService () from onBind ():

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(); } ... } 

Call startForegorund () from onCreate ():

 public class ExampleService extends Service { ... @Override public IBinder onBind(Intent intent) { return mBinder; } @Override public void onCreate() { super.onCreate(); ... startForeground(ord, note); } ... } 

Call startForegorund () from onCreate () and startService from onBind ():

 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); } ... } 

Clients will use this service calling bindService () and using IBinder.

+4
source share
1 answer

I have more information about these 3 use cases, I tried these three cases, and for each of them I took “dumpsys activity processes” after starting the application, the associated service and transferring activity in the background (pressing the home button):

The first case (only call startService () in onBind ()):

 *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) 

The second case (only call startForeground () in onCreate ()):

 *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) 

The third case (call: startService () in onBind () and startForeground () in onCreate ()):

 *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) 

The default case (does not call startService () and startForeground (), but only binds the service):

 *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) 

I think the most important value is adj , which changes in all cases and takes these values:

1) svc

2) prcp

3) bak

0
source

Source: https://habr.com/ru/post/1482823/