2012-03-14 36 views
0

我試圖在後臺自動刷新JmDNS服務。當我嘗試什麼也沒有發生:自動刷新活動中的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按鈕,否則此代碼不會執行任何操作。我試圖用線程嘗試它,但活動關閉,應用程序也關閉。有任何想法嗎?

回答

0

首先對您的代碼稍加評論。你爲什麼用三種不同的方法三次執行相同的代碼。我假設你正在編程的android(在你的方法名稱loking)。每次活動開始或恢復時,都會執行onresume方法。有關此主題的更多信息,請參閱this link

好吧,那麼......您是否已經檢查了文檔以獲得關於ScheduledExecutorService的更多信息?

現在爲jmdns問題。 jmDns庫具有內置功能來更新服務。您可以收聽新服務並在新服務可用時採取適當措施。我不認爲,重複投票的服務是正確的方法。

請看here關於在an​​droid中使用jmdns的一點教程。