我试图让我的 Android 应用程序在应用程序处于活动状态时通过 zeroconf 注册服务,然后在应用程序退出后取消注册。在我的 onResume 方法中,我有以下代码:
// register ourselves with zero conf
try {
JmDNS jmdns = JmDNS.create();
serviceInfo = ServiceInfo.create("_http._tcp.local.", "My App Service", SERVER_PORT, 0, 0, false, "path=/crazycatapp");
jmdns.registerService(serviceInfo);
} catch (Throwable t) {
Debug.e("Error registering service with zeroconf", t);
}
然后在我的 onPause 方法中:
if(serviceInfo != null) {
// unregister ourselves with zero conf
JmDNS jmdns = JmDNS.create();
jmdns.unregisterService(serviceInfo);
}
} catch (Throwable t) {
Debug.e("Error unregistering service with zeroconf", t);
}
我已经删除了启用多播的代码等。关键是,我可以很好地注册服务,然后可以在另一台设备上使用 zeroconf 浏览器查看它。但是,一旦注册,该服务就永远不会消失,除非我真的禁用了注册该服务的设备上的 wifi。对 unregisterService 的调用似乎并不真正影响服务注册。unregisterAllServices 似乎也没有任何效果。有人有想法么?