有两个应用A和B,通过aidl来来共享服务,现在有个问题: 
当我想把服务Destroy的时候,为什么要A和B都unbind后,才会Destroy;为什么不是谁create的,就由谁Destroy?????? 你们能看懂我说的吗????? 

解决方案 »

  1.   

    服务不管是启动几次,都只有一个service在运行,谁create的,create了几次这个并没有什么关系,同一个service只有一个。
      

  2.   

     private ServiceConnection conn=new ServiceConnection(){
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    // TODO Auto-generated method stub
        
    Log.d("ling","bind service .....");
    player=IPlayer.Stub.asInterface(service);
    try {
    player.addFile("");
    player.setName("zhangling");
    Log.d("ling",""+player.ToString());

    }
    catch (RemoteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    } @Override
    public void onServiceDisconnected(ComponentName name) {
    // TODO Auto-generated method stub
    unbindService(conn);

    }
    当service销毁时候,连接就会断开,回调OnserviceDisconnected这个函数,然后解除绑定,最后销毁service
      

  3.   

    都没有看懂我在问什么这才是正确答案:
    public abstract boolean stopService (Intent service) Since: API Level 1 
    Request that a given application service be stopped. If the service is not running, nothing happens. Otherwise it is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it was started. 
    Note that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed. See the Service documentation for more details on a service's lifecycle.