6

I'm writing a SIP application using an SDK. My application has many Activities and so I initialise and shutdown the SDK using a service's onCreate and onDestroy methods so that I can run it for the duration of my Application (not just an individual Activity).

The example application I'm working from calls startService and then just leaks the service - I don't want to do this but I'm not sure that I have an alternative.

What I would like to do is bind to the service using Context.BIND_AUTO_CREATE in my Activity base class's OnCreate method and unbind in OnDestroy. All of my Activities extend from this, so I would then have the Service available in all of my Activities.

But, what happens when the configuration changes or I switch activities? Does the service get aggressively killed in the brief period between activities, or is there a guarantee that it won't? If it's the former, doesn't that slightly cripple the use of services? What design pattern should I be using to make something persist for exactly the lifetime of my Application?

pjcard
  • 1,088
  • 10
  • 22

2 Answers2

3

I've realised I'm asking the wrong question. I'm assuming the Application will be destroyed when the user finishes with it, which it probably won't, it'll just sit in the background, and so will my service. I think I need a redesign.

Furthermore if I really wanted to continue down that path (which I now don't), I could bind the service to my application, it would then exist for the lifetime of the application (which, as I remembered, will be indefinite unless the user kills it or Android reclaims it). In this instance I don't need to specifically call unbind, as the binding will be destroyed along with the application.

pjcard
  • 1,088
  • 10
  • 22
2

In Activities, start the Service using startService() and also bind to the service. This will keep your service alive if there is a config change.

Unbind from the service in the onDestroy() of your Activities. The service will be still running as you started it not just bound to it.

Keep a static counter variable in your launcher activity. Increase it by 1 for other activity launch in its onCreate() and decrease it by one in their onDestroy(). So, if 3 activities have been opened and assuming that you are keeping the other 2 in backstack, static counter is three. Now in onDestroy() of each decrease the counter and check if it is zero, stop the service.

In my FirstActivity (Launcher), I have public static int counter = 0;

then in each Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FirstActivity.counter++;
}

@Override
protected void onDestroy() {
    super.onDestroy();

    if (--FirstActivity.counter == 0) {
        stopService(new Intent(this, MyService.class));
    }
}
smholloway
  • 589
  • 7
  • 14
Akhil
  • 13,888
  • 7
  • 35
  • 39
  • And how would I call stopService in the manner you suggest? There is no way to detect the Application closing, if there was I wouldn't need a service, I'd just put it in the Application! – pjcard Apr 16 '12 at 06:15
  • And i say "yes", there is way. For getting Application Lifecycles, you need to make a custom Application class extending Application and registering it is manifest. I forgot to mention about it in the answer. And yes, you may not need a service at all since you are limiting it to the app lifecycle. See ans by Reto Meier here : http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android – Akhil Apr 16 '12 at 06:31
  • Sorry Akhil, that's still not right. I already have a custom application class but it has no method for detecting the Application exiting. See my "Additional" section on the question for why this is. – pjcard Apr 16 '12 at 07:03
  • To be more specific, there's no OnDestroy, and OnTerminate is only "for use in emulated process environments." – pjcard Apr 16 '12 at 09:35
  • I didnt know that about onTerminate() and based my answer on that. I am suggesting an alternative in the updated answer below. hope that will be useful. – Akhil Apr 17 '12 at 15:01
  • It seems that this counting approach is not so good.. :S What u people think about checking if user interact with UI (activities) and if nothing happen for some time (few minutes lets say) service is calling stopSelf(), so it doesn't waste memory, battery til next lunch from activity..hmm? – Ewoks Jun 19 '12 at 11:46