android在网络方面提供下面的功能:提供无线连接网络的方式,使用网络发现服务和Wi-Fi创建点到点的连接。使用XML格式来交换网络数据。在下载和网络交换中,如何减少电池的消耗。

在云方面,可以同步和备份用户数据。

下面是具体内容:

1.网络服务发现允许你的用户发现和识别本地网络的其他设备,这在文件共享和多个玩家的游戏中非常实用。android的NSD API简单的提供了这样的功能。下面讲述如何广播自己的用户名和连接信息,同时可以扫描其他的设备。

public void registerService(int port) {
// Create the NsdServiceInfo object, and populate it.
NsdServiceInfo serviceInfo = new NsdServiceInfo();

// The name is subject to change based on conflicts
// with other services advertised on the same network.
serviceInfo
.setServiceName("NsdChat");
serviceInfo
.setServiceType("_http._tcp.");
serviceInfo
.setPort(port);
....
}

public void initializeServerSocket() {
// Initialize a server socket on the next available port.
mServerSocket
= new ServerSocket(0);

// Store the chosen port.
mLocalPort
= mServerSocket.getLocalPort();
...
}

public void initializeRegistrationListener() {
mRegistrationListener
= new NsdManager.RegistrationListener() {

@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
// Save the service name. Android may have changed it in order to
// resolve a conflict, so update the name you initially requested
// with the name Android actually used.
mServiceName
= NsdServiceInfo.getServiceName();
}

@Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Registration failed! Put debugging code here to determine why.
}

@Override
public void onServiceUnregistered(NsdServiceInfo arg0) {
// Service has been unregistered. This only happens when you call
// NsdManager.unregisterService() and pass in this listener.
}

@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Unregistration failed. Put debugging code here to determine why.
}
};
}

public void registerService(int port) {
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo
.setServiceName("NsdChat");
serviceInfo
.setServiceType("_http._tcp.");
serviceInfo
.setPort(port);

mNsdManager
= Context.getSystemService(Context.NSD_SERVICE);

mNsdManager
.registerService(
serviceInfo
, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}

上面是注册的代码。下面是发现网络的代码:

public void initializeDiscoveryListener() {

// Instantiate a new DiscoveryListener
mDiscoveryListener
= new NsdManager.DiscoveryListener() {

// Called as soon as service discovery begins.
@Override
public void onDiscoveryStarted(String regType) {
Log.d(TAG, "Service discovery started");
}

@Override
public void onServiceFound(NsdServiceInfo service) {
// A service was found! Do something with it.
Log.d(TAG, "Service discovery success" + service);
if (!service.getServiceType().equals(SERVICE_TYPE)) {
// Service type is the string containing the protocol and
// transport layer for this service.
Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
} else if (service.getServiceName().equals(mServiceName)) {
// The name of the service tells the user what they'd be
// connecting to. It could be "Bob's Chat App".
Log.d(TAG, "Same machine: " + mServiceName);
} else if (service.getServiceName().contains("NsdChat")){
mNsdManager
.resolveService(service, mResolveListener);
}
}

@Override
public void onServiceLost(NsdServiceInfo service) {
// When the network service is no longer available.
// Internal bookkeeping code goes here.
Log.e(TAG, "service lost" + service);
}

@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "Discovery stopped: " + serviceType);
}

@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mNsdManager
.stopServiceDiscovery(this);
}

@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mNsdManager
.stopServiceDiscovery(this);
}
};
}

mNsdManager.discoverServices(
SERVICE_TYPE
, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);

mNsdManager.discoverServices(
SERVICE_TYPE
, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);

连接到网络服务:

public void initializeResolveListener() {
mResolveListener
= new NsdManager.ResolveListener() {

@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Called when the resolve fails. Use the error code to debug.
Log.e(TAG, "Resolve failed" + errorCode);
}

@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
Log.e(TAG, "Resolve Succeeded. " + serviceInfo);

if (serviceInfo.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same IP.");
return;
}
mService
= serviceInfo;
int port = mService.getPort();
InetAddress host = mService.getHost();
}
};
}

当应用程序关闭时,注销网络服务:

//In your application's Activity

@Override
protected void onPause() {
if (mNsdHelper != null) {
mNsdHelper
.tearDown();
}
super.onPause();
}

@Override
protected void onResume() {
super.onResume();
if (mNsdHelper != null) {
mNsdHelper
.registerService(mConnection.getLocalPort());
mNsdHelper
.discoverServices();
}
}

@Override
protected void onDestroy() {
mNsdHelper
.tearDown();
mConnection
.tearDown();
super.onDestroy();
}

// NsdHelper's tearDown method
public void tearDown() {
mNsdManager
.unregisterService(mRegistrationListener);
mNsdManager
.stopServiceDiscovery(mDiscoveryListener);
}

未完待续............

更多相关文章

  1. 如何做好 Android(安卓)端音视频测试?
  2. Android常用名令集锦(图文并茂)(转:来自网络)
  3. Android(安卓)可视化性能监控和辅助小工具合集
  4. Android(安卓)App 网络接入实时监控
  5. android 实现从网络上抓取图片并显示在手机上
  6. 为Android、iOS设备搭建模拟丢包、慢速网络模拟环境
  7. 腾讯微博获得oauth_verifier后跳转失败
  8. Android(安卓)内存剖析 – 发现潜在问题
  9. android的 网络下载和数据持久化

随机推荐

  1. 想找好用的BI软件?看这一篇就够了:2021年好
  2. 浅谈MySQL索引...
  3. 三层口聚合技术笔记 (华为肖哥)
  4. 将Hexo部署到自己的服务器上
  5. 华三IRF 堆叠 精讲版 笔记(肖哥)
  6. K8s手动方式搭建平台及问题汇总
  7. 【免费】华为认证考试资料,肖哥视频课程整
  8. 《商君书》白话翻译与解读——13章 靳令
  9. 《商君书》白话翻译与解读——20章 弱民
  10. 入群领取华为HCIE学习资料包,抢4天直播训