开发者

Android - Are services shared among processes?

I have an android library which is distributed as a jar file for inclusion in 3rd party applications.

Within the jar file is an Android Service. The service is exposed through a facade class in the jar file. Hence 3rd parties do not directly bind to the service, they just use the facade class.

I have 2 questions about this architecture:

1) If two completely separate 3rd party applications use this facade 开发者_如何学C(different package names, different user ids etc..) class are two instances of the service created or is a single instance shared?

2) Does it make a difference if the service is bound directly to the 3rd party apps rather than accessed thru a facade?

P.S. http://developer.android.com/reference/android/app/Service.html Does not answer these questions.


The answer is that only one instance of the service will be running at a time (no matter what, unless you jump through some crazy hoops to make it run multiple times). Multiple calls to Context.startService() and onCreate() do not create additional instances of it. If you are needing to do anything when a 3rd party connects, it should be passed via onStartCommand or the Binder object returned from Context.bindService().

Your model is independent of the underlying Service handling in Android, the behavior would be the same whether your facade was accessing the Service or if 2+ 3rd parties were accessing the Service.


OK i did a quick test on this.

I modified the provided jar file to store values passed in the onBind Intent into 2 static variables. (stAppID and stAppVer) and print them out when a certain method is called on the service

When binding to the service each app passes in different values with the Intent.

If the service is indeed shared between applications I would expect 2 things:

  1. Process id is the same when statics are printed.
  2. After both apps have bound to the service subsequent calls to the method which prints the static variables should print out the same value.

Here is the log output from my test.


E/************ ServiceTest *************(  744): ***************************************************
E/************ ServiceTest *************(  744): ***************************************************
E/************ ServiceTest *************(  744): Thread = main
E/************ ServiceTest *************(  744): stAppID = APP AAAAAAAAAAAAA
E/************ ServiceTest *************(  744): stAppVer = VER AAAAAAAAAAAAA
E/************ ServiceTest *************(  744): ***************************************************
E/************ ServiceTest *************(  744): ***************************************************
D/dalvikvm(  744): GC freed 4963 objects / 267800 bytes in 100ms
D/dalvikvm(  778): GC freed 2640 objects / 151400 bytes in 105ms
I/ActivityManager(  578): Starting activity: Intent { flags=0x10100000 comp={com.a.service.test/com.a.service.test.TrackingSample} }
W/InputManagerService(  578): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4370dac0
I/ActivityManager(  578): Starting activity: Intent { flags=0x10100000 comp={com.b.service.test/com.b.service.test.TrackingSample} }
W/InputManagerService(  578): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@436f7760
D/MMT     (  778): Creating new last event timestamp record for UIApplicationWillTerminateNotification
D/MMT     (  778): Run closed
D/MMT     (  778): Run started
E/************ ServiceTest *************(  778): ***************************************************
E/************ ServiceTest *************(  778): ***************************************************
E/************ ServiceTest *************(  778): Thread = main
E/************ ServiceTest *************(  778): stAppID = APP BBBBBBBBBBBBBBB
E/************ ServiceTest *************(  778): stAppVer = VER BBBBBBBBBBBBBBB
E/************ ServiceTest *************(  778): ***************************************************
E/************ ServiceTest *************(  778): ***************************************************
E/************ ServiceTest *************(  744): ***************************************************
E/************ ServiceTest *************(  744): ***************************************************
E/************ ServiceTest *************(  744): Thread = main
E/************ ServiceTest *************(  744): stAppID = APP AAAAAAAAAAAAA
E/************ ServiceTest *************(  744): stAppVer = VER AAAAAAAAAAAAA
E/************ ServiceTest *************(  744): ***************************************************
E/************ ServiceTest *************(  744): ***************************************************
D/dalvikvm(  744): GC freed 836 objects / 131136 bytes in 86ms

As can be seen from this. Neither of the required conditions have held true therefore I conclude that the service is not being shared between applications. (the process ID is the number in parenthesis just before the colon in each log line)

This makes sense when you think about it. How can the operating system decide that the repackaged jars contain the same version of the service. There is no version metadata declared on an Android service.

Another problem is this service makes use of a database. If that database was shared among applications that would be a security risk.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜