Android、解除绑定服务和onServiceDisconnected问题

发布于 2024-10-21 21:42:47 字数 1094 浏览 3 评论 0原文

我的英语不好,但我会尝试以良好的方式解释我的问题。

所以,问题是:
1) 我有本地服务
2)我启动它然后绑定它。 3)当我要关闭该服务时出现问题。我的 ServiceConnection 类实现中的 onServiceDisconnected 方法从未被调用。如果我手动关闭它(从设置中),或通过 unbindService、stopService、或 unbindService 和 stopService 的组合 - onServiceDisconnected 仍然不会被调用。 我做错了什么?

简短的代码如下:

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);
}

我正在 Android 2.2 的模拟器下测试此代码

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女猫性小仙女 2024-10-28 21:42:47

onServiceDisconnected 仅在极端情况下才会被调用(崩溃/终止)。

对于本地服务来说,这种情况不太可能发生,因为所有应用程序组件通常都在同一个进程中运行...这意味着,除非您有意解除绑定或销毁服务,否则它应该保持连接,或者与使用它的组件一起死亡。

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.

谈场末日恋爱 2024-10-28 21:42:47

Android 开发者文档说...

public abstract void onServiceDisconnected (ComponentName name)

当与服务的连接丢失时调用。当托管服务的进程崩溃或被终止时,通常会发生这种情况。这不会删除 ServiceConnection 本身 - 与服务的绑定将保持活动状态,并且当服务下次运行时,您将收到对 onServiceConnected(ComponentName, IBinder) 的调用。

有关更多信息:https://developer.android。 com/reference/android/content/ServiceConnection#onServiceDisconnected(android.content.ComponentName)

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)

落叶缤纷 2024-10-28 21:42:47

我在bindService中使用了Context.BIND_AUTO_CREATE。在应用它对我来说完美的工作后,我遇到了同样的问题。

bindService(new Intent(this,XMPPService.class),mConnection, Context.BIND_AUTO_CREATE);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文