android activity调用service

  private class MyServiceConnection implements ServiceConnection {
 
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
 
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            proxy = IMyAidlInterface.Stub.asInterface(service);
            try {
                service.linkToDeath(mDeathRecipient, 0);        // 注册死亡代理
                if(proxy!=null){
                    socketManager  = proxy.getName();
                    socketManager.checkout();
                    HoxinApplication.getInstance().setSocketManager(socketManager);
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };
    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
 
        @Override
        public void binderDied() {                  // 当绑定的service异常断开连接后,会自动执行此方法
            if (proxy != null){
                proxy.asBinder().unlinkToDeath(mDeathRecipient, 0);
                bindService();
            }
        }
    };

 

你可能感兴趣的