有点问题的背景。我正在制作一个使用网络服务发现来广播服务的多人游戏。虽然我既不是 java 也不是 android 的初学者,但我是使用 Wi-Fi 连接设备的技术的初学者。因此,在我的应用程序中,我有一个活动,用户提供进入房间或创建一个房间。当用户点击“创建房间”时,我首先检查是否有已经注册的服务。如果存在,我取消注册它并注册一个新的,我还将这些操作的结果写入调试日志。这是一个方法的代码,在“创建房间”按钮的 onClick() 方法中调用:
public void registerService(){
if(mConnection.getLocalPort() > -1) {
mNsdHelper.tearDown();
mNsdHelper.registerService(mConnection.getLocalPort());
mNsdHelper.discoverServices();
Log.d(Keys.MAFIA_TAG, "Service Registered from registerService()");
} else {
Log.d(Keys.MAFIA_TAG, "ServerSocket isn't bound.");
}
}
tearDown() 方法代码:
public void tearDown() {
if (mRegistrationListener != null) {
try {
mNsdManager.unregisterService(mRegistrationListener);
} finally {
}
mRegistrationListener = null;
}
}
问题是:正如我从日志中看到的那样,该服务已成功取消注册,并且在它之后系统告诉我发现了相同的服务,所以现在我有两个服务:新创建的和旧的。以下是日志:
用户按下“创建房间”:
05-06 20:30:10.362 3685-3685/com.example.yarkov.mafiamultiplayer D/mafia main﹕ DeviceSony //the name of the service
05-06 20:30:10.363 3685-3685/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service registration started
05-06 20:30:10.366 3685-3685/com.example.yarkov.mafiamultiplayer D/mafia main﹕ Service Registered from registerService()
05-06 20:30:10.378 3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service discovery started
05-06 20:30:10.433 3685-3685/com.example.yarkov.mafiamultiplayer I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2bdc45ef time:32538709
05-06 20:30:11.180 3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service registered: DeviceSony
05-06 20:30:11.431 3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Same machine: DeviceSony //a service has been found, but it's a local one
用户按下返回按钮:
05-06 20:30:35.440 3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service unregistered: DeviceSony
05-06 20:30:35.563 3685-3685/com.example.yarkov.mafiamultiplayer I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@35979eff time:32563839
05-06 20:30:36.649 3685-4701/com.example.yarkov.mafiamultiplayer E/NsdHelper﹕ service lostname: DeviceSony, type: _http._tcp., host: null, port: 0
用户再次按下“创建房间”:
05-06 20:31:16.867 3685-3685/com.example.yarkov.mafiamultiplayer I/Timeline﹕ Timeline: Activity_launch_request id:com.example.yarkov.mafiamultiplayer time:32605143
05-06 20:31:16.935 3685-3685/com.example.yarkov.mafiamultiplayer D/mafia main﹕ DeviceSony
05-06 20:31:16.935 3685-3685/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service registration started
05-06 20:31:16.935 3685-3685/com.example.yarkov.mafiamultiplayer D/mafia main﹕ Service Registered from registerService()
05-06 20:31:16.940 3685-5996/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service discovery started
05-06 20:31:17.016 3685-3722/com.example.yarkov.mafiamultiplayer D/OpenGLRenderer﹕ endAllStagingAnimators on 0xb83c6600 (RippleDrawable) with handle 0xb83ae018
05-06 20:31:17.028 3685-3685/com.example.yarkov.mafiamultiplayer I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@4d088df time:32605304
05-06 20:31:17.624 3685-5996/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service registered: DeviceSony
05-06 20:31:17.872 3685-5996/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Same machine: DeviceSony
05-06 20:31:17.875 3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Same machine: DeviceSony
我们可以看到找到了 2 个服务而不是一个,这意味着第一个服务没有正确取消注册。
谢谢大家!