0

I am using Service to do long running process. I bind the service in activity to initiate the process. Below is the code which binds the service.

val intent = Intent(context,SaveSessionService::class.java)
context.bindService(intent, mSaveSessServiceConnection, AppCompatActivity.BIND_AUTO_CREATE)

After the service finishes at the background, I am trying to stop the service. I am using stopService(). But I am getting activity leaking service connection error. After doing some reading on SO Questions, came to know that when service is bound with BIND_AUTO_CREATE, we can't stop the service with stopService.

My Question is how can I connect with the service as I also read that calling startService for long running service is also not advisable. Is it better to use IntentService (I don't think so as only Service should be used for long running task as per the documentation)?

I can't reference back to activity, once I goto service. I should be able to stop the service from service itself.

So with this use case, which is better, to start a service with startService or use IntentService?

User
  • 1,186
  • 4
  • 22
  • 36

1 Answers1

0
  1. If you have started the service with bindService() then it should stop automatically after you call unbindService() if there are no other bindings.

  2. If you start the service with startService() then you should stop it with stopService() or the service can call stopSelf() after it is done. This is often the preferred approach.

  3. If it would be better to use IntentService depends on what functionality the service provides - the question does not have any details about this.

  4. As mentioned earlier you can stop the service from the service itself with stopSelf() as mentioned earlier

RocketRandom
  • 1,102
  • 7
  • 20