在Android/多线程上实现JmDNS
我正在尝试让 JmDNS 在我的 android 程序中工作。我能够让它发现我想要的设备,但我不完全理解如何将信息从 JmDNS 获取到启动 JmDNS 任务的对象。这是我的代码。
protected void browse() {
try {
jmdns = (JmDNSImpl) JmDNS.create();
jmdns.addServiceListener(type, listener = new ServiceListener() {
public void serviceResolved(ServiceEvent ev) {
}
public void serviceRemoved(ServiceEvent ev) {
}
public void serviceAdded(ServiceEvent event) {
DNSEntry addressEntry = jmdns.getCache().getDNSEntry(name, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY);
if (addressEntry instanceof DNSRecord) {
ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(true);
if (cachedAddressInfo != null) {
for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
//I need to get the address that is here back out of this listener to the main thread
}
}
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
我遇到的问题是我有一个服务管理器对象,它有一个浏览器对象的实例,其中有 browser 方法。我无法让服务管理器对象访问地址变量。因为 JmDNS 在创建它来运行其任务时会产生自己的线程,所以我尝试使用处理程序和可运行程序来发送带有变量的消息,但我似乎无法正确处理。有人可以帮忙吗?
I am trying to get JmDNS to work in my android program. I am able to get it to discover the devices I want, but I do not fully understand how to get the information from JmDNS to the object that started the JmDNS task. Here is my code.
protected void browse() {
try {
jmdns = (JmDNSImpl) JmDNS.create();
jmdns.addServiceListener(type, listener = new ServiceListener() {
public void serviceResolved(ServiceEvent ev) {
}
public void serviceRemoved(ServiceEvent ev) {
}
public void serviceAdded(ServiceEvent event) {
DNSEntry addressEntry = jmdns.getCache().getDNSEntry(name, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY);
if (addressEntry instanceof DNSRecord) {
ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(true);
if (cachedAddressInfo != null) {
for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
//I need to get the address that is here back out of this listener to the main thread
}
}
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
The problem I am running into is that I have a service manager object that has a instance of a browser object that has the browse method in it. I am unable to get the service manager object access to the address variable. Because JmDNS spawns its own thread when it is created to run its tasks I have tried to use a handler and runnable to send messages with the variable in it but I cant seem to get it right. Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您只想使用传递到服务添加方法中的 ServiceEvent 事件对象。它有您需要的所有信息。
中的示例
请参阅我们的开源应用程序http://code.google.com/p/tunesremote-plus/source/browse/trunk/src/org/tunesremote/LibraryActivity.java
I think you want to just use the ServiceEvent event object passed into the service added method. It has all the info you need.
See this example from our open source application
http://code.google.com/p/tunesremote-plus/source/browse/trunk/src/org/tunesremote/LibraryActivity.java