26
  1. Is it possible to start an IntentService on a separate process? How? If so, is it mandatory to bind to it?
  2. Is it possible to start an IntentService on a separate process AND run it in the foreground?
  3. What's the difference between android:isolatedProcess and android:process? See: http://developer.android.com/guide/topics/manifest/service-element.html
Andrés Pachon
  • 838
  • 1
  • 7
  • 15
  • Using extra processes is not a good idea, as it wastes RAM and CPU, usually to no benefit for the user. – CommonsWare Sep 19 '12 at 22:31
  • @CommonsWare: Roger that sir! Is there any case in which you'd recommend using a separate process? – Andrés Pachon Sep 20 '12 at 04:15
  • In general multiple process are wasteful. However, I can think of a few cases where they are useful: 1) You need more memory than you can get in a single process - so you split your application into multiple process because memory limits are per-process. 2) You have a service that needs to run all the time and a large UI that doesn't - You put the UI in one process that Android can kill off when not needed and the service in another process that runs all the time (this actually turns out to be better use of RAM/CPU). – David Wasser Sep 20 '12 at 09:44
  • 1
    3) Part of your application is stable and part of it is flakey/buggy or subject to issues or downloaded from some website - You run the stable code in one process and the other stuff in another, so that when the flakey code crashes it doesn't take the stable part with it. – David Wasser Sep 20 '12 at 09:45
  • 1
    @DavidWasser: You are welcome to cite proof of your claims regarding reason #2. #3 is insufficient justification for a second process on a slow, low-memory mobile device IMHO. I will grant you that #1 was apropos for Android 1.x/2.x, but `android:largeHeap` was added for API Level 11 and higher, to give you more heap space without requiring another process, and too many developers think "oh, give me more heap" is a solution for inefficient coding. – CommonsWare Sep 20 '12 at 10:46
  • @CommonsWare - I think what David Wasser meant by "better use of RAM/CPU" is not that it magically uses less resources total, but that it is better exactly because the UI can be killed by OS if necessary, without killing your essential code. So your app can accomplish what it needs to do with a minimum footprint. Then when the user brings the UI to foreground, more will have been accomplished, despite the limited resources. [The user will have done whatever else it was they wanted to do ~and~ your app will have done its work for the user.] – ToolmakerSteve Sep 22 '16 at 15:35
  • @CommonsWare - Or does Android kill all processes of an app if it kills any of them? In which case, this technique is obsoleted by `android:largeHeap`, as you suggest. – ToolmakerSteve Sep 22 '16 at 15:40
  • @ToolmakerSteve: "Or does Android kill all processes of an app if it kills any of them?" -- no, AFAIK it treats processes individually in general. – CommonsWare Sep 22 '16 at 15:58

1 Answers1

37

1) Is it possible to start an IntentService on a separate process? How? If so, is it mandatory to bind to it?

Yes, you can start an IntentService in a separate process. Simply add android:process=":whatever" to the manifest entry for that service.

No, you don't need to bind to it. You can communicate with it by sending it Intents using startService()

2) Is it possible to start an IntentService on a separate process AND run it in the foreground?

Yes (see above). To make your service run in the foreground it can call startForeground() whenever it wants to do that. The service itself is in control of whether it runs in the foreground or background.

3) What's the difference between android:isolatedProcess and android:process? See: http://developer.android.com/guide/topics/manifest/service-element.html

android:process allows you to control in which process each particular component runs (by specifying the name of the process). You can group components of your application to run in separate processes (for example, all UI components in one process and all services in another). The default behaviour is that all components of an application run in the same process.

android:isolatedProcess is a flag (true/false) that you can set if you want a particular service component to run in a separate process isolated from the rest of your application. The isolated process doesn't have any of the permissions that are granted to the rest of your application. Normally, permissions are granted to an application and all components of the application have all the permissions that the application gets. android:isolatedProcess is only available starting with API level 16 (Jellybean). See http://aleksmaus.blogspot.de/2012/09/a-feature-of-android-jelly-bean.html and Advantage of introducing Isolatedprocess tag within Services in JellyBean[Android]

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274