0

I have a regular Android ServiceConnection:

I'm overriding the onServiceDisconnected() method like so:

private ServiceConnection myConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        System.out.println("MyTag why are you not getting called");
    }
};

and the service is binded and started just fine. i intentionally crash the app after some time after the service is connected; however, the onServiceDisconnected is never called - why is that? according to the documentation, this is supposed to get called whenever an app crashes...

the thing that i'm trying to accomplish is this:

suppose the user just flat out kills the process/ or accidentally runs into a crash, i want certain things to happen (like... cancelling a notification... which is what i currently do in the activity's onStop()). but where should i cancel the notification from? well also to disconnect service if the app crashed?

David T.
  • 22,301
  • 23
  • 71
  • 123
  • I guess [this](http://stackoverflow.com/questions/971824/when-does-serviceconnection-onservicedisconnected-get-called) is what you are looking for. – Dariush Malek Apr 16 '14 at 23:40

1 Answers1

2

I've never seen onServiceDisconnected(...) called when an app crashes, or even when an activity unbinds from a service. I think it's only called if the service dies though no action of the bound activity, perhaps because the OS decided to kill it to save memory. I don't know how to intentionally cause that to happen. You could try adding a deliberate crash to your service and see what happens, but I think that would just crash your whole app unless the service is running in its own process.

I had a similar need to make sure some things happen if an app crashes or is killed, because Bluetooth on many devices is extremely flaky, and if a Bluetooth socket is open when an app crashes, the adapter can be left in a state where it cannot connect to the same device/uuid again until the phone or tablet is rebooted. I found that a service's onDestroy() is also not called when an app crashes, unless the service is running in its own process. If you decide to go that route, beware of the fact that the service will inherit its own copy of your Application class, so if you've extended it to hold global data, the service will have all its own copies.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • yeah... see the problem is that in all of the "onStop()" calls, the stuff i want to happen doesn't happen when the app crashes – David T. Apr 17 '14 at 02:12