在Android/多线程上实现JmDNS

发布于 2024-10-26 18:47:39 字数 1365 浏览 6 评论 0原文

我正在尝试让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2024-11-02 18:47:39

我认为您只想使用传递到服务添加方法中的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文