自动刷新活动中的JmDNS服务
我试图在后台刷新" title="自动刷新">自动刷新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按钮,否则此代码不会执行任何操作。我试图用线程尝试它,但活动关闭,应用程序也关闭。有任何想法吗?
回答:
首先对您的代码稍加评论。你为什么用三种不同的方法三次执行相同的代码。我假设你正在编程的android(在你的方法名称loking)。每次活动开始或恢复时,都会执行onresume方法。有关此主题的更多信息,请参阅this link。
好吧,那么......您是否已经检查了文档以获得关于ScheduledExecutorService的更多信息?
现在为jmdns问题。 jmDns库具有内置功能来更新服务。您可以收听新服务并在新服务可用时采取适当措施。我不认为,重复投票的服务是正确的方法。
请看here关于在android中使用jmdns的一点教程。
以上是 自动刷新活动中的JmDNS服务 的全部内容, 来源链接: utcz.com/qa/263764.html