Android provides the following functions in terms of Network: it provides a wireless connection method, and uses the network discovery service and Wi-Fi to create point-to-point connections. Use XML format to exchange network data. How to reduce battery consumption during download and network switching.
On the cloud side, you can synchronize and back up user data.
The specific content is as follows:
1. Network Service Discovery allows your users to discover and recognize other devices on the local network, which is very useful in file sharing and multiple player games. Android nsd api provides such a simple function. The following describes how to broadcast your username and connection information and scan other devices.
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 ){
// Save the service name. Android may have changed it in order
// 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 );
}
The above is the registered code. The code for network discovery is as follows:
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 cocould 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 );
Connect to the network service:
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 ();
}
};
}
When the application is closed, log out of the network service:
// 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 );
}