5

Three questions concerning processes in Android. I am developing one app. If I declare a service to be running in another process in AndroidManifest.xml

<service android:name=".MyService" android:process=":MyProcess"/>
  1. Does it mean there will be two JVM instances , one used by MyService while the other one used by other code?

  2. If the answer of above question is YES, then does it also mean if I have a singleton class used by both Activity & MyService, then there will be two instances created for the singleton?

  3. How to ensure only one instance be created & shared by two processes then? Better provide a sample :)

====UPDATE====

Thanks for all your comments and answer(s), unfortunately, my project needs to use separate service process as it has long running background tasks. According to your replies, I have a 4th question:

If I need to pass a non-parcelable object across MyService process and the other code's process. Is it possible? And how?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • 1. there is no JVM on android ... 2. thats why storing data in singletons are bad on android ... 3. do not use singleton ... use normal IPC methods supported by android's framework(small Parcelables(in Bundles, Intents), ContentProviders, Binders) – Selvin Feb 24 '15 at 10:18
  • I don't know about all three concern but i found good information about process here, This link may help you. http://stackoverflow.com/questions/7142921/usage-of-androidprocess. – IshRoid Feb 24 '15 at 10:27
  • @Leem.fin You'll need to make the non-parcelable objects parcelable : -) See for example [this](http://stackoverflow.com/questions/7181526/how-can-i-make-my-custom-objects-be-parcelable) answer – Joris Mar 01 '16 at 12:48

1 Answers1

5
  1. Does it mean there will be two JVM instances , one used by MyService while the other one used by other code?

yes (in a certain way, but not exactly, it's a dalvik VM, or in Lollipop ART), but yeah, you have two separate things running the service and rest of code.

  1. If the answer of above question is YES, then does it also mean if I have a singleton class used by both Activity & MyService, then there will be two instances created for the singleton?

yes

  1. How to ensure only one instance be created & shared by two processes then? Better provide a sample :)

you can't! You just told the system to have separate processes. So it cannot have "the same" singleton.

An approach to that is have your service implement a binder or AIDL (in case you to direct calls methods) or implement a ContentProvider, which is the same across processes that you can read values out of it.

Or you can just make it all simpler and NOT use the process. 99.9% of use cases android:process is not advised. So re-evaluate your software. Do you really need it?

edit:

unfortunately, my project need to use separate service process as it has long running background tasks

If your project need a long running background task, you need a Service for sure. But it doesn't mean it needs to be in a separate process. All the activities from your project can go to background and be garbage collected and your service still runs fine. It will keep running on the same process as the Activities were running but the Service and singletons will still be alive. Just remember to return START_STICKY from it.

If you still think you need to use it in a separate process (I don't think you need). Then you need to implement a bound service (or maybe AIDL, I'm not sure bound service will work across process), connect the activity to it and give the reference of the object using a normal method like public void takeThisObJect(Object reference);

links to bound and AIDL service guides:

http://developer.android.com/guide/components/bound-services.html http://developer.android.com/guide/components/aidl.html

dcanh121
  • 4,665
  • 11
  • 37
  • 84
Budius
  • 39,391
  • 16
  • 102
  • 144
  • see my edit. and remember AIDL is very complicate. And Bound is a little less complicate. You certainly want to keep in the same process. – Budius Feb 24 '15 at 10:57
  • thanks, but as I know AIDL only accept parcelable object to be passed around. My question in the "UPDATE" section is actually asking about how to pass non-parcelable object with AIDL. – Leem.fin Feb 25 '15 at 14:22
  • I actually was not aware of that limitation of AIDL, I just read through the link and it's true. So there you go. That's your answer. What you're trying to do cannot be done because Android cannot pass non primitive data types across processes. As I said on my edited answer, your Service doesn't have to be in a different `process` and in 99.99% of cases the `process` tag shouldn't ever be used. Give it a try without it, it will work just fine. – Budius Feb 25 '15 at 14:39
  • I'm just thinking more on your problem, there's a hack work around that might allow you to pass objects, but that will also be limited as your object cannot have any external connections (e.g. database connection or be processing a data-stream). You can make the object you want to pass `serializable`, right it to the disk and then call an `IPC` method to notify the other process. The other process then can deserialize the object. – Budius Feb 25 '15 at 14:41