gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:52:18 26 4
gpt4 key购买 nike

有人遇到同样的错误并可以解决它吗?

java.lang.IllegalArgumentException: listener already in use at 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.
}
}

最佳答案

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

像这样:

nsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, new NsdManager.RegistrationListener() {...});

附注服务解析也必须做同样的事情。

在此处查看更多信息:docs

关于java - Android NSD(网络服务发现)注册监听器已在使用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47797493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com