1

I have 2 apps with the same SharedUserId value in the manifest, App1 and App2. App1 has a Service component which is specified to run in a separate process (in the manifest android:process=":remote"). The main purpose of this service is to check on App2. I would like the service to do some cleanup when App2 closes.

When I force close App2, App1's service is killed, and its onDestroy() method is not called. How can I ensure that App1's service is not killed (or at least finishes with calling onDestroy) when I force stop App2?

edit: Android 4.0.1 on Galaxy Nexus if that makes a difference

Matt
  • 985
  • 2
  • 9
  • 22

1 Answers1

0

Yes that is possible. If you are using sharedUserId imagine that those 2 applications run in the same thread which it means they have the same application life cycle which basically means that with the "force close" on any of these applications you terminate their thread.

Meaning that everything on this thread is killed. One of the negative things in using sharedUserID and sharedProcess between applications rather than using content providers.

I don't think there is a work around when you are force closing the app. Maybe you should just finish it the "normal" way and let the os kill it.

UPDATE from your comment:

In general I don't tend to intervene with these kind of stuff. The user might want to manually close the app, as I am used to doing, and it would be irritating if you have a service or the app running again and again.

That's my point of view. Despite my belief, there is a trick to manually relaunch your application after force closing when an unhandled exception had fired and I guess the reaction of the force close by user would be the same. You could add the pending intent in the service I guess: Android: How to auto-restart application after it's been "force closed"?

Community
  • 1
  • 1
10s
  • 1,699
  • 1
  • 12
  • 14
  • If it were up to me I would allow the os to kill it. I am just dealing with the case where a user decides to kill it him/herself. Does sharedUserId force the apps to be in the same process? The [documentation](http://developer.android.com/guide/topics/manifest/manifest-element.html) indicates that they will run in the same process "if desired". – Matt Jul 04 '12 at 19:46
  • Yes that is true, don't you have the same sharedProcess in your applications? I have noticed that in order for sharing the resources in 2 applications you should include the same process attribute in both applications. Despite sharedUserID alone is working fine with 2.x versions, in 3.x and some 4.0 it was not working. I guess it is some new security features. Don't you have the same issues? – 10s Jul 05 '12 at 10:51
  • I do not have the same sharedProcess in my apps. Data sharing works just fine (I am able to read and write the files of App2 from App1). – Matt Jul 05 '12 at 14:11
  • have you tested it on a 3.x version device? And by files you mean database access, preferences or just files saved in inner storage? – 10s Jul 05 '12 at 15:11
  • No, I haven't tested it on a 3.x device. I guess I could try it out on an emulator. I have just been accessing files in local storage (including database files, but not making any calls to those databases with SQLite) – Matt Jul 05 '12 at 16:51
  • The emulator of 3.x is a pain even though you are trying to load it on a powerful machine. Sometimes it doesn't even boot. Good luck with that. You didn't respond to my other question, are you accessing the database or just some files in the application private storage? – 10s Jul 05 '12 at 18:35
  • I edited my comment but perhaps it didn't show up for you. I have just been accessing files in local storage (including database files, but not making any calls to those databases with SQLite). – Matt Jul 05 '12 at 19:23
  • Ok, then it is a different case than mine. To sum up, the os seems to kill both apps so it must be that the applications are running on the same process. I don't know that particular answer on how to disable the same process "thing" but as I posted above, the edited comment, I have a very good alternative perception to what you are trying to do. Hope I helped – 10s Jul 05 '12 at 20:38
  • It's very strange because the Logcat window shows that the two apps have different PIDs. Maybe the android OS is killing my service for memory reasons? Doesn't really seem likely on a GNexus with nothing else running, and my apps aren't that memory-intensive. Thanks for the link. I was looking at that as well. Is it possible to place that code block in a Service? It seemed to me that it needs to go in an Activity. That would be problematic for me because my service is not launched at startup but only following some user input. – Matt Jul 06 '12 at 15:37
  • as provided by the link you can place it in your application class too. If you don't make use of a custom application class then just make one in order to place that code. If the application "closes" that code will regenerate the application itself. It is a very common trick for application recovery after unhandled exceptions. – 10s Jul 06 '12 at 15:50