0

I want to start a service when the users open my application, some of my activities need to bind the service, and when the users close my application, I stop the service.

The question is where can I stop the service. I want my service work when my app is open.

I start the service in Application.onCreate() method, and I found there is no onDestroy() method in Application.

And I tried stop it in the launcher activity onDestroy, but I don't think this is a good way because the system will recovery launcher activity when low memory.

L. Swifter
  • 3,179
  • 28
  • 52
  • "I want to start a service when the users open my application, some of my activities need to bind the service, and when the users close my application, I stop the service" -- what is the service giving you, that you could not handle by some ordinary Java object? – CommonsWare Sep 21 '16 at 15:22
  • I want to maintain a TCP connection with the server when the app is open, some activities need the socket to send message. I think this is a work for service, I can't find an other way to do this until now. – L. Swifter Sep 21 '16 at 15:33
  • 1
    "I can't find an other way to do this until now" -- use a regular Java object, in the form of a singleton `AppConnectionManager` or something. Again: why does this need to be a service? – CommonsWare Sep 21 '16 at 15:40
  • 1
    The system will not recover Launcher Activity when low memory unless it kills the whole process, in which case it would be moot. So if it would work in Launcher onDestroy() otherwise it would be fine. – Steve M Sep 21 '16 at 16:27
  • @CommonsWare thanks, I will try to use a singleton class to do this. – L. Swifter Sep 22 '16 at 01:39

1 Answers1

0

What if you started and stopped your service based on running activities count? Here's an example.

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {

    private int startedActivitiesCount = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
    }

    private void onAppStart() {
        // Start service
    }

    private void onAppQuit() {
        // Stop service
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        startedActivitiesCount++;
        if (startedActivitiesCount == 1) {
            onAppStart();
        }
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        startedActivitiesCount--;
        if (startedActivitiesCount == 0) {
            onAppQuit();
        }
    }

    @Override
    public void onActivityStarted(Activity activity) {
    }

    @Override
    public void onActivityResumed(Activity activity) {
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityStopped(Activity activity) {
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    }

}
Egis
  • 5,081
  • 5
  • 39
  • 61