Question

I'm trying to automatically refresh JmDNS services in the background. Nothing is happening when I try:

@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();
}

This is my resfreshServices() method:

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();
}

Normally that gets call when a button is clicked however I would like it to be automatic. This code doesn't do anything unless I hit the Resfresh button that call resfreshServices(). I attempted to try it with threads but the activity closes and so does the app. Any ideas?

Was it helpful?

Solution

First a little comment on your code. Why are you implementing the same code three times in three different methods. I assume that you are programming android (loking at your method names). The method onresume is executed every time the activity is started or resumed. See this link for more information on this topic.

Ok, then... Did you already check the docs for more information about the ScheduledExecutorService?

Now for the jmdns issue. The jmDns library has a build in functionality to update the services. you can listen to new services and take the appropriate action when new services are available. I do not think, that repeated polling of the services is the right approach.

Look here for a little tutorial on using jmdns in android.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top