首页 文章

如何解除特定服务的绑定?

提问于
浏览
1

我有一个绑定服务,反过来使用相同的ServiceConnection对象绑定多个服务 . 在onServiceConnected中,我保存了Map中每个服务的ComponentName和Binder,以便我可以单独使用它们 . 在某一点上,我想分开解除其中一些服务 . 有没有办法在Android中执行此操作?

我能够找到取消绑定服务的唯一方法是使用unbindService(ServiceConnection),但我认为我不能使用它解除绑定特定服务 .

为什么这似乎不受支持?有任何缺点吗?

1 回答

  • 0

    这是绑定和解除绑定服务的示例代码

    i = new Intent(this, YourService.class)
    
    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.bindService(i, mServerConn, Context.BIND_AUTO_CREATE);
    mContext.startService(i);
    }
    
    public void stop() {
    mContext.stopService(new Intent(mContext, ServiceRemote.class));
    mContext.unbindService(mServerConn);
    }
    

相关问题