Android、解除绑定服务和onServiceDisconnected问题
我的英语不好,但我会尝试以良好的方式解释我的问题。
所以,问题是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
Android 开发者文档说...
public abstract void onServiceDisconnected (ComponentName name)
有关更多信息:https://developer.android。 com/reference/android/content/ServiceConnection#onServiceDisconnected(android.content.ComponentName)
Android developer documentation says...
public abstract void onServiceDisconnected (ComponentName name)
For more: https://developer.android.com/reference/android/content/ServiceConnection#onServiceDisconnected(android.content.ComponentName)
我在bindService中使用了Context.BIND_AUTO_CREATE。在应用它对我来说完美的工作后,我遇到了同样的问题。
I used
Context.BIND_AUTO_CREATE
in bindService. I have fetch same issue after apply its working perfect for me.