sandalone
sandalone

Reputation: 41749

How to implement a service that runs only when the app is running?

The app has a service which has to detect how many minutes the app is running and based on that, the service will initiate misc actions.

What is the proper way to implement this?

How can I be sure the service is running ONLY when the app is running in front of the user?

Starting the service seems easy - just start it on splash loading. But the harder part is ending it. I cannot just end it when the user press Back button on the last screen. How to handle situation when a user presses Home screen or some other other app (like phone call, or viber popup, or...) takes over the screen?

I tried taking suggestions from the other theme (How to start a android service from one activity and stop service in another activity?), but this does not handle the situation with Home button or other app taking over the screen.

The app has in total around 10 activities. Is it a proper way to bind this service to all 10 activities and when all are off, the service then turn itself off?

Upvotes: 0

Views: 1323

Answers (2)

Karakuri
Karakuri

Reputation: 38595

Make a BaseActivity for all of your Activities. In the BaseActivity, do the following:

public class MyActivity extends Activity implements ServiceConnection {

    //you may add @override, it's optional
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, this, 0);
    }

    //you may add @override, it's optional
    protected void onStop() {
        super.onStop();
        unbindService(this);
    }

    public void onServiceConnected(ComponentName name, IBinder binder) {};
    public void onServiceDisconnected(ComponentName name) {};

    /* lots of other stuff ... */
}

Your BaseActivity will need to implement ServiceConnection interface (or you can use an anonymous inner class), but you can leave those methods empty.

In your Service class, you need to implement the onBind(Intent) method and return an IBinder. The easiest way to do that is like so:

public class MyService extends Service {
    private final IBinder localBinder = new LocalBinder();

    public void onCreate() {
        super.onCreate();
        // first time the service is bound, it will be created
        // you can start up your timed-operations here
    }

    public IBinder onBind(Intent intent) {
        return localBinder;
    }

    public void onUnbind(Intent intent) {
        // called when the last Activity is unbound from this service
        // stop your timed operations here
    }

    public class LocalBinder extends Binder {

        MyService getService() {
            return MyService.this;
        }
    }
}

Upvotes: 2

Kai
Kai

Reputation: 15476

Bound Service is define specifically for this purpose, you can bind Activities to it, and when all the Activities are gone, it will be stopped as well. The link should contain enough detail for you to implement.

Upvotes: 1

Related Questions