Android的网络服务发现协议NSD)能够用于在小范围的网络中发现邻近设备上的某个应用。这对于一些社交网络、多人游戏类的应用会很有帮助。
Android的NSD的用法大致上分为四种操作:
1. 注冊网络服务
2. 发现网络服务
3. 连接网络服务
4. 注销网络服务
使用NSD时一定要注意:
记得在Manifest中增加android.permission.INTERNET
权限,不然程序会崩溃。
一. 注冊网络服务
注冊网络服务须要两样东西: 网络服务的信息NsdServiceInfo)和注冊事件监听器NsdManager.RegistrationListener)
这两样东西齐全后就能够通过:NsdManager.registerService发放来注冊网络服务了。
实例代码例如以下:
public void registerServiceView view) {
// 注意:注冊网络服务时不要对端口进行硬编码,通过例如以下这样的方式为你的网络服务获取
// 一个可用的端口号.
int port = 0;
try {
ServerSocket sock = new ServerSocket0);
port = sock.getLocalPort);
sock.close);
} catch Exception e) {
Toast.makeTextgetApplicationContext), "can not set port", Toast.LENGTH_SHORT);
}
// 注冊网络服务的名称、类型、端口
NsdServiceInfo nsdServiceInfo = new NsdServiceInfo);
nsdServiceInfo.setServiceName"NSD_Test_Program");
nsdServiceInfo.setServiceType"_http._tcp.");
nsdServiceInfo.setPortport);
// 实现一个网络服务的注冊事件监听器。监听器的对象应该保存起来以便之后进行注销
nsRegListener = new NsdManager.RegistrationListener) {
@Override
public void onUnregistrationFailedNsdServiceInfo arg0, int arg1) {
Toast.makeTextgetApplicationContext), "Unregistration Failed", Toast.LENGTH_SHORT).show);
}
@Override
public void onServiceUnregisteredNsdServiceInfo arg0) {
Toast.makeTextgetApplicationContext), "Service Unregistered", Toast.LENGTH_SHORT).show);
}
@Override
public void onServiceRegisteredNsdServiceInfo arg0) {
Toast.makeTextgetApplicationContext), "Service Registered", Toast.LENGTH_SHORT).show);
}
@Override
public void onRegistrationFailedNsdServiceInfo arg0, int arg1) {
Toast.makeTextgetApplicationContext), "Registration Failed", Toast.LENGTH_SHORT).show);
}
};
// 获取系统网络服务管理器,准备之后进行注冊
NsdManager nsdManager = NsdManager) getApplicationContext).getSystemServiceContext.NSD_SERVICE);
nsdManager.registerServicensdServiceInfo, NsdManager.PROTOCOL_DNS_SD, nsRegListener);
}
注意:registerService)方法是异步运行的,假设有一定要在服务注冊完成后才干运行的操作,请在onServiceResgistered事件中执这些操作。
二. 发现网络服务
要发现附近的网络服务须要定义一个网络服务发现时间监听器。代码例如以下:
public void discoverServiceView view) {
nsDicListener = new NsdManager.DiscoveryListener) {
@Override
public void onStopDiscoveryFailedString serviceType, int errorCode) {
Toast.makeTextgetApplicationContext), "Stop Discovery Failed", Toast.LENGTH_SHORT).show);
}
@Override
public void onStartDiscoveryFailedString serviceType, int errorCode) {
Toast.makeTextgetApplicationContext),
"Start Discovery Failed", Toast.LENGTH_SHORT).show);
}
@Override
public void onServiceLostNsdServiceInfo serviceInfo) {
Toast.makeTextgetApplicationContext), "Service Lost", Toast.LENGTH_SHORT).show);
}
@Override
public void onServiceFoundNsdServiceInfo serviceInfo) {
// 发现网络服务时就会触发该事件
// 能够通过switch或if获取那些你真正关心的服务
Toast.makeTextgetApplicationContext), "Service Found", Toast.LENGTH_SHORT).show);
}
@Override
public void onDiscoveryStoppedString serviceType) {
Toast.makeTextgetApplicationContext), "Discovery Stopped", Toast.LENGTH_SHORT).show);
}
@Override
public void onDiscoveryStartedString serviceType) {
Toast.makeTextgetApplicationContext), "Discovery Started", Toast.LENGTH_SHORT).show);
}
};
NsdManager nsdManager = NsdManager) getApplicationContext).getSystemServiceContext.NSD_SERVICE);
nsdManager.discoverServices"_http._tcp", NsdManager.PROTOCOL_DNS_SD,nsDicListener);
}
三. 连接网络服务
通过定义一个网络服务连接时间监听器来轮询解析到的网络服务。能够进一步获取该网络服务的地址和port然后决定是否进行连接,演示样例代码:
public void initResolveListenerView view) {
nsResolveListener = new NsdManager.ResolveListener) {
@Override
public void onServiceResolvedNsdServiceInfo arg0) {
// 能够再这里获取对应网络服务的地址及port信息,然后决定是否要与之建立连接。
// 之后就是一些socket操作了
}
@Override
public void onResolveFailedNsdServiceInfo arg0, int arg1) {
}
};
}
四. 注销网络服务
想要注销网络服务,应该事先保存和该网络服务有关的句柄。通过NsdManager.unregisterService和NsdManager.stopServiceDiscovery方法来注销网络服务。实例代码:
public void unregisterServiceView view) {
NsdManager nsdManager = NsdManager) getApplicationContext).getSystemServiceContext.NSD_SERVICE);
nsdManager.stopServiceDiscoverynsDicListener); // 关闭网络发现
nsdManager.unregisterServicensRegListener); // 注销网络服务
}
假设转载请注明出处:http://blog.csdn.net/gophers
