Android, unbind service and onServiceDisconnected problem
Asked Answered
C

3

18

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

Cozmo answered 13/3, 2011 at 22:39 Comment(0)
P
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.

Pentose answered 13/3, 2011 at 22:55 Comment(3)
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.Cozmo
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().Exoteric
@yokks,Did u find the answer for this?Holter
A
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)

Amateurish answered 13/7, 2020 at 16:19 Comment(0)
D
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);
Dictionary answered 28/2, 2017 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.