Android NSD(网络服务发现)注册监听器已在使用中

时间:2017-12-13 16:07:25

标签: java android nsd

任何人都面临同样的错误并可以解决它?

  

java.lang.IllegalArgumentException:已在使用的侦听器   在android.net.nsd.NsdManager.registerService(NsdManager.java:485)

NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setServiceName(myServiceName);
serviceInfo.setServiceType(AppConfig.NSD_SERVICE_TYPE);
serviceInfo.setPort(AppConfig.VIDEO_CALL_PORT);
nsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener);

...

NsdManager.RegistrationListener registrationListener = new NsdManager.RegistrationListener() {

        @Override
        public void onServiceRegistered(NsdServiceInfo serviceInfo) {
            try {
                // Android may have changed it in order to resolve a conflict, so update the name
                myServiceName = serviceInfo.getServiceName();
                nsdManager.discoverServices(AppConfig.NSD_SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, discoveryListener);
            } catch (Exception e) {
                AppUtil.logErrorBaseOnMode(e);
            }
        }

        @Override
        public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
            // todo
            // Registration failed!  Put debugging code here to determine why.
        }

        @Override
        public void onServiceUnregistered(NsdServiceInfo arg0) {
            // todo
            // Service has been unregistered.  This only happens when you call NsdManager.unregisterService() and pass in this listener.
        }

        @Override
        public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
            // todo
            // Un-registration failed.  Put debugging code here to determine why.
        }
    }

1 个答案:

答案 0 :(得分:1)

我相信你只需要为每次注册使用新的监听器。 每次启动服务注册时,都必须创建新的RegistrationListener对象。

像这样:

ResponseEntity

P.S。必须为服务解决做同样的事情。

在此处查看更多信息:docs