本文介绍了Android的,解除绑定的服务和onServiceDisconnected问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是英语不好,但我会尽量解释好办法我的问题。

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

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

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?

短code是如下:

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

我测试下的Andr​​oid 2.2的模拟器这个code

I'm testing this code under emulator of Android 2.2

推荐答案

onServiceDisconnected只调用在极端情况下(坠毁/打死)。

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

这是不太可能发生的本地服务,因为所有的应用程序组件通常在同一个进程中运行...意义,除非你intentionnaly解除或销毁服务,它应该保持连接,或将其与组件模具

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.

这篇关于Android的,解除绑定的服务和onServiceDisconnected问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 05:32