1

I have a service in Android that encapsulates a framework that has a start method. The service boils down to something like this, many things omitted:

public class MyService extends Service {

    private IBinder thisBinder;

    public MyService(){
        thisBinder = new LocalBinder();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return thisBinder;
    }

    public void start(Map<String, Object> options)
    {
        getDriverManager().start(options);
    }
}

I also have a bridging class that makes calls to the service:

public class MyServiceBridge implements ServiceConnection {

    private boolean started = false;
    private boolean bound = false;
    private MyService myService;

    public MyServiceBridge(Context context){
        this.context = context;
    }

    public void bindService(){
        Intent intent = new Intent(getContext(), MyService.class);

        getContext().bindService(intent, this, getContext().BIND_AUTO_CREATE);
        getContext().startService(intent);
    }

    // Here's a sample call, and the one that is relevant
    public void start(Map<String, Object> options){
        setOptions(options);
        if(bound == true){
            getMyService().start(options);
        }
        else{
            started = true;
        }
    }
}

I call the bridge's start method in order to run the service. This works fine, except in this particular situation (so far). The MyApplication class calls the bridge's start method on onCreate:

public class MyApplication extends Application {

    @Override
    public void onCreate() {

        super.onCreate();

        getServiceBridge().start(null);
    }
}

This, according to the docs is "Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.". Indeed it appears to be so, because the service does not start, and instead starts when I close the app (odd, at least). This works if I move the call to an activity's onCreate method, but that's not ideal because I can also stop the service, and I want the service to run for the lifetime of the app. That is, the service should start when the app starts and stop when the app terminates. Does this make sense? Is there another way to do this?

André Fratelli
  • 5,920
  • 7
  • 46
  • 87
  • `onCreate ` method is called or not of Application class? – ρяσѕρєя K Jun 22 '16 at 09:14
  • Yes, the method gets called! However, this and @vanste25's answer made me realise that it might have something to do with the service being still running on the background. I say this because the method does not seem to be called the second time I run the app. – André Fratelli Jun 22 '16 at 09:25

1 Answers1

2

In my opinion, you did a good job when you decided to run service in Application onCreate. but it is strange to hear that service is started when you close the app. I have done this several times, and service starts in application onCreate which must be called.

Have you checked if your application is alive and run in background? Please make sure that you killed you application before testing. Use Task Killer or something like that, to be sure that application is always freshly started.

Sadly, Android does not have appropriate mechanism to notify you when application is exited, because it is still alive until system decides to kill it and free resources.

vanste25
  • 1,754
  • 14
  • 39
  • I see, so services _do_ work if started there. That excludes what I though it was. I could really use a method for when the application (or the service) is terminated, though. This app is a bit cleanup-sensitive (it uses external devices, such as routers). Now that I know that services can be started there I'll give it some more time for debug. Thanks! – André Fratelli Jun 22 '16 at 09:29
  • You welcome. Watch out for calling startService and bindService sequentially getContext().bindService(intent, this, getContext().BIND_AUTO_CREATE); getContext().startService(intent); Maybe there is your problem. – vanste25 Jun 22 '16 at 09:31
  • Really? Why is that? – André Fratelli Jun 22 '16 at 09:38