Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I share a service between activities without it restarting?

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?

like image 393
pjcard Avatar asked Apr 16 '12 05:04

pjcard


People also ask

Can a service start another service in Android?

You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.

What does startActivity do?

Starting an activity You can start a new instance of an Activity by passing an Intent to startActivity() . The Intent describes the activity to start and carries any necessary data. If you want to receive a result from the activity when it finishes, call startActivityForResult() .

What is onCreate Method in android?

onCreate is used to start an activity. super is used to call the parent class constructor. setContentView is used to set the xml.

How do I start a service from activity?

A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. A service is bound when an application component binds to it by calling bindService().


2 Answers

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.

like image 127
pjcard Avatar answered Nov 15 '22 03:11

pjcard


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));
    }
}
like image 38
Akhil Avatar answered Nov 15 '22 04:11

Akhil