0

I have implemented a simple Android Service that, by default, is deployed within the same process as my app / apk. I want the Service running concurrently with each Activity. To make that happen, in each Activity.onStart() and Activity.onStop() implementation, I have logic that invokes Activity.bindService() and Activity.unbindService(), respectively.

Well, all of this works fine, but it feels awkward. Is there any other way to make sure the Service is continuously running and bound to all Activities without having to re-invoke Activity.bindService() and Activity.unbindService() for each Activity? Should the Service in this case be declared as a stand-alone process?

Also, my Service starts a separate thread, but never stops it. Should my code stop the thread? Is there a chance the thread could be orphaned? Starting / stopping the thread with OnUnbind / OnRebind seems like overkill.

Kode Charlie
  • 1,297
  • 16
  • 32
  • create a base Activity that binds/unbinds the Service and make all your Activities extend this base Activity – pskink Jul 07 '15 at 17:48
  • Here is a discussion on Stackoverflow that suggests to use the approach pskink mentioned: http://stackoverflow.com/questions/2621395/more-efficient-way-of-updating-ui-from-service-than-intents/2622473#2622473 – devz Jul 07 '15 at 18:08
  • If your service only does tasks from time to time consider using IntentService – Alexander Kulyakhtin Jul 07 '15 at 18:09

3 Answers3

0

You can start your Service in your custom Application class. This way the service will be started only when your application is started.

For example:

public class MainApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        starService();
    }

    public void starService() {
        Intent i = new Intent(this, YourService.class);
        this.startService(i);
    }
}
Gunhan
  • 6,807
  • 3
  • 43
  • 37
0

While the other answers ere good, you might want to ask this: "Does this service needs to keep on running while the application is not, and will not run?"

  • If so: create as an independant service
  • If not: extend a helper class that implements the bind/unbind and have Activities extend that.
Bonatti
  • 2,778
  • 5
  • 23
  • 42
0

Create a base Activity and call bindService in onStart, unbindService in onStop.

public class BaseActivity extends Activity {

    @Override  
    public void onStart() {
        // ...
        bindService(intent, serviceConnection, flags);
    }

    @Override
    public void onStop() {
        // ....
        unbindService(serviceConnection);    
    }
}

This will make sure every activity that extends base is bound to the service.

When last activity is unbound from the service, it will be stopped. If you want to avoid that, call startService first, and then bind to it. This will prevent service from stopping even if you don't have running activities.

Should the Service in this case be declared as a stand-alone process?

In your case, you don't need a separate process for your service.

Also, my Service starts a separate thread, but never stops it. Should my code stop the thread?

If you want to stop your service, you should stop your thread because thread is a GC root, and all objects accessible from it will remain in memory. So, infinite thread that is not used is a memory leak.

You can implement threading different ways depending on your requirements. You can either implement a regular thread in your Service, or a ThreadPoolExecutor or a Handler. Pick a solution that fits to your needs.

Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41