17

I'm not good in English, but I would try to explain my problem in good way.

So, the problem is:
1) I have a local service
2) I start it and then bound to it. 3) Problem appears when I am about to close that service. onServiceDisconnected method from my implementation of class ServiceConnection is never called. If I close it manually (from settings), or by unbindService, or by stopService, or by combination of unbindService and stopService - onServiceDisconnected still doesn't to be called. What am I doing wrong?

Short code is below:

protected ServiceConnection mServerConn = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        Log.d(LOG_TAG, "onServiceConnected");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.d(LOG_TAG, "onServiceDisconnected");
    }
}

public void start() {
    // mContext is defined upper in code, I think it is not necessary to explain what is it 
    mContext.bindService(i, mServerConn, Context.BIND_AUTO_CREATE);
    mContext.startService(i);
}

public void stop() {
    mContext.stopService(new Intent(mContext, ServiceRemote.class));
    mContext.unbindService(mServerConn);
}

I'm testing this code under emulator of Android 2.2

UAS
  • 405
  • 2
  • 4
  • 9

3 Answers3

31

onServiceDisconnected is only called in extreme situations (crashed / killed).

which is very unlikely to happen for a local service since all your application components normally run in the same process... meaning, unless you intentionnaly unbind or destroy the service, it should remain connected, or die with the component using it.

darma
  • 4,687
  • 1
  • 24
  • 25
  • 1
    Ok, thanks. I didn't think, that it is a bug or smth else. So I copied needle data (code) from onServiceDisconnected() to stop(). Now works as I've wanted. – UAS Mar 14 '11 at 11:33
  • 1
    this is correct. but after calling unbindService(connection) also, am able to call the public method present in the myservice(service) class. so, there is no meaning with the unbindservice() method right??? But the service is destroyed by calling onDestroy(). – yokks Mar 27 '11 at 17:25
  • @yokks,Did u find the answer for this? – kavie Jul 22 '21 at 17:57
2

Android developer documentation says...

public abstract void onServiceDisconnected (ComponentName name)

Called when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed. This does not remove the ServiceConnection itself -- this binding to the service will remain active, and you will receive a call to onServiceConnected(ComponentName, IBinder) when the Service is next running.

For more: https://developer.android.com/reference/android/content/ServiceConnection#onServiceDisconnected(android.content.ComponentName)

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Ali Cv
  • 21
  • 3
1

I used Context.BIND_AUTO_CREATE in bindService. I have fetch same issue after apply its working perfect for me.

bindService(new Intent(this,XMPPService.class),mConnection, Context.BIND_AUTO_CREATE);
Carl Poole
  • 1,970
  • 2
  • 23
  • 28
Sagar Jethva
  • 986
  • 12
  • 26