1

I have an android application which starts a service in the main activity with this command:

bindService(new Intent(ctx, MyService.class), _connection, Context.BIND_AUTO_CREATE);

The main activty does excatly nothing with this service (thats why I didn´t provide the code for the Service connection). The idea is that the main activity handels the service lifetime!

It "stops" the service with this code after the user clicks the "close" button of my app:

if (_service != null) {
    unbindService(_connection);
}
//Some cleanup here ...
System.exit(0);

I know at this point that no other activity is binded to the service cause every activity is called from the main activity and they unbind from the service at "onStop".

So the problem is that my Service wont call "onDestroy" when the user closes the app! When I remove the "System.exit(0)" line it will close normally!

How can I make sure that the service is closed properly in this situation?

EDIT:

Ok I read this discussion: Is quitting an application frowned upon?.

Now I rebuild my activities. I have one activity that calls the service with startService, after that it binds the service. When the activity is destroyed it calls unbindService.

The service itself returns START_STICKY in onStartCommand to make it harder to kill it. It calls stopSelf when all work is done.

Now I have the problem that I get the ServiceConnectionLeaked message if the binding still exists! Whats about that? How could I prevent this?

Community
  • 1
  • 1
Nightmare
  • 53
  • 5

1 Answers1

0

Use finish() instead of System.exit(0)

mach
  • 8,315
  • 3
  • 33
  • 51