15

I have an Android app that consists of an activity and a service. Currently they both exist in the same process and use the same heap but I want have to separate process/heap for the service. Ie. I want the service to be completely independent of the activity so that if the activity crashes it won't affect the service. I do, however, want them to be installable as a single application. Is this possible?

chris
  • 1,731
  • 4
  • 26
  • 33

2 Answers2

22

Definitely possible. See the process attribute for service in AndroidManifest.xml

http://developer.android.com/guide/topics/manifest/service-element.html

To quote:

The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The element's process attribute can set a different default for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

Community
  • 1
  • 1
lyricsboy
  • 2,999
  • 24
  • 21
  • Nice. @lyricsboy but a Question: in OP´s scenario, when Android kills the Activity (hence the app), will it also have killed the running Service if it is on the same process? – Orkun Feb 26 '12 at 23:44
  • It depends on how the service is started (which mode) and other factors related to memory. You can find more specifics at: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html – lyricsboy Feb 27 '12 at 00:11
  • By element do you mean **application**?. Which permission is required by service to run globally? – Malwinder Singh Jun 19 '15 at 10:05
5

IPC for services is IMHO only required if the service should be consumed by other applications.

Running a service in its own process has the small advantages that the garbage collector for the service does not affect your application and that the memory footprint of the service is a bit smaller if it runs alone.

If consumption of the service by other applications is not a requirement for you, prefer a local service. Alternatively you can still run the service in its own process and use different communication with your application, e.g. via a broadcast receiver. I tried to describe the different approaches in my Android service tutorial under the following link: Activity and service communication.

vogella
  • 24,574
  • 4
  • 29
  • 26
  • isn't the (service in separated process)the only way to prevent the service from call its own onDestroy when calling activity destroyed?? – EsmaeelQash Feb 11 '14 at 09:27
  • Hello Vogella, can you please explain me any scenerio in which we neet to "consumption of the service by other applications" , i am not getting this term. thanks – SRam Mar 25 '14 at 13:04
  • Running a service in a separate process helps ensure that, should the process of the main app be terminated, the service can continue. So this is quite useful. – Luca May 31 '14 at 21:57
  • 1
    Another advantage of running a service in its own process is that if user explicitly ends the application process the service will still keeps on running. – Muhammad Babar Feb 22 '16 at 10:55