0

我正在尝试在后台自动刷新 JmDNS 服务。当我尝试时什么都没有发生:

@Override
public void onDestroy() {
    try {
        hiNeighborService.unregisterListener(this);
        this.unbindService(this.serviceConnection);

    } catch (Exception ex) {
        Log.e(LOG_TAG, "Exception occur during destroying the app.");
    }

    super.onDestroy();
}

@Override
public void onStart() {
    /*new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                refreshServices();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } 

    }).start();*/

    ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);

    // This schedule a runnable task every 2 minutes
    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
      public void run() {
        rebindService();
        refreshServices();
      }
    }, 0, 1, TimeUnit.SECONDS);


    super.onStart();
}

@Override
public void onRestart() {
    /*new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                refreshServices();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } 

    }).start();*/
    ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);

    // This schedule a runnable task every 2 minutes
    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
      public void run() {
        rebindService();
        refreshServices();
      }
    }, 0, 1, TimeUnit.SECONDS);

    super.onRestart();
}

@Override
public void onResume() {
    /*new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                refreshServices();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } 

    }).start();*/

    ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);

    // This schedule a runnable task every 2 minutes
    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
      public void run() {
        rebindService();
        refreshServices();
      }
    }, 0, 1, TimeUnit.SECONDS);

    super.onResume();
}

这是我的 resfreshServices() 方法:

private void refreshServices() {
    Log.i(LOG_TAG, "Refresh available neighbors...");
    final List<Neighbor> activeNeighbors = this.hiNeighborService
            .getActiveNeighbors();
    Log.d(LOG_TAG, activeNeighbors.size() + " active neighbors are found!");

    new Thread(new Runnable() {
        public void run() {
            Log.i(LOG_TAG, "refresh UI...");
            try {

                synchronized (activeNeighborsViewModel) {
                    activeNeighborsViewModel.clear();
                    for (Neighbor neighbor : activeNeighbors) {
                        NeighborViewModel vm = new NeighborViewModel(
                                neighbor);
                        vm.setNeighborUnreadCount(ConnectActivity.this
                                .getUnreadMessageCount(neighbor));
                        if (activeNeighborsViewModel.contains(vm)) {
                            activeNeighborsViewModel.remove(vm);
                        }

                        activeNeighborsViewModel.add(vm);
                    }
                }
                notifyServiceListChanged();

                Log.i(LOG_TAG, "refresh completed!");
            } catch (Exception ex) {
                ex.printStackTrace();
                Log.e(LOG_TAG, ex.toString());
            }
        }
    }).start();
}

通常,当单击按钮时会调用它,但是我希望它是自动的。除非我点击调用 resfreshServices() 的 Resfresh 按钮,否则此代码不会执行任何操作。我试图用线程尝试它,但活动关闭了,应用程序也关闭了。有任何想法吗?

4

1 回答 1

0

首先对你的代码做一点评论。你为什么用三种不同的方法实现相同的代码三次。我假设您正在编程 android(查看您的方法名称)。每次启动或恢复活动时都会执行 onresume 方法。有关此主题的更多信息,请参阅此链接

好的,那么...您是否已经查看文档以获取有关ScheduledExecutorService的更多信息?

现在针对 jmdns 问题。jmDns 库具有更新服务的内置功能。您可以收听新服务并在新服务可用时采取适当的措施。我不认为,重复轮询服务是正确的方法。

在此处查看有关在 android 中使用 jmdns 的小教程。

于 2012-07-25T12:04:43.337 回答