Understanding AsyncTask – Once and Forever

Motivation

Android modifies the user interface via one thread, the so called UI Thread. If you perform a long running operation directly on the UI Thread, for example downloading a file from the internet, the user interface of your application will “freeze” until the corresponding task is finished. When this happens it is very easy for the user to perceive your application as slow.

As a concrete example of a bad implementation and what happens when a long running operation is done on the UI Thread, I want to refer to one of my previous tutorials: Creating A Simple RSS Application in Android. Well, that application is working fine, and it does what it is supposed to do – parse an XML feed and display the headlines in a ListView. The “vulnerability” of that application is that the network access is done directly on the UI Thread which makes the application to “freeze” while the XML feed is downloaded (take a look at point number 5 to see).
When I created that tutorial I wanted to make it as simple as possible without dealing with more advanced topics like asynchronous tasks. The intent of tutorial was to show the working process with feeds on a high level. But I promise you, by the end of this article you will be able to fix it and have a Cool Rss App that runs smoothly! 🙂

To provide a good user experience all long running operations in an Android application should run asynchronously. To achieve this we will be using the AsyncTask class.

What does AsyncTask do?

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. 

In order to use the AsyncTask class, you must extend it and override at least thedoInBackground() method.

The most common methods you will need to implement are these:

   1. onPreExecute() – called on the UI thread before the thread starts running. This method is usually used to setup the task, for example by displaying a progress bar.

   2. doInBackground(Params…) – this is the method that runs on the background thread. In this method you should put all the code you want the application to perform in background. Referring to our Simple RSS Aplication, you would put here the code that downloads the XML feed and does the parsing. The doInBackground() is called immediately after onPreExecute(). When it finishes, it sends the result to the onPostExecute().

   3. onProgressUpdate() – called when you invoke publishProgress() in the doInBackground().

   4. onPostExecute(Result) – called on the UI thread after the background thread finishes. It takes as parameter the result received from doInBackground().

AsyncTask is a generic class, it uses 3 types: AsyncTask<Params, Progress, Result>.

  1. Params – the input. what you pass to the AsyncTask
  2. Progress – if you have any updates, passed to onProgressUpdate()
  3. Result – the output. what returns doInBackground()

Once a task is created, it can be executed like this:
new DownloadTast().execute(url1, url2, urln);

Code Example

This is a simple skeleton of an AsyncTask implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class AsyncTaskTestActivity extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      //Starting the task. Pass an url as the parameter.
      new PostTask().execute("http://feeds.pcworld.com/pcworld/latestnews");
   }
   // The definition of our task class
   private class PostTask extends AsyncTask<String, Integer, String> {
   @Override
   protected void onPreExecute() {
      super.onPreExecute();
      displayProgressBar("Downloading...");
   }
   @Override
   protected String doInBackground(String... params) {
      String url=params[0];
      // Dummy code
      for (int i = 0; i <= 100; i += 5) {
        try {
          Thread.sleep(50);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
         publishProgress(i);
      }
      return "All Done!";
   }
   @Override
   protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      updateProgressBar(values[0]);
   }
   @Override
   protected void onPostExecute(String result) {
      super.onPostExecute(result);
      dismissProgressBar();
   }
   }
}

AsyncTasks are great for performing tasks in a separate thread, they have however one weakness. While the AsyncTask is in the middle of the work and the screen of device is rotated, you’ll notice that the application crashes. This happens because when rotating the device screen a configuration change occurs, which will trigger the Activity to restart. The AsyncTask reference to the Activity is invalid, an onPostExecute() will have no effect on the new Activity. How to handle this sort of issues is described in: Dealing with AsyncTask and Screen Orientation, which I highly recommend reading it if you are concerned to deliver stable Android applications.

How to build a server in Java: Allowing multiple users to connect

Servers with multiple connections

Right now, our server allows one user to connect and then just prints out what that user has sent to the server. Now that’s cool but it isn’t really very useful. Wouldn’t it be awesome if multiple connections would be allowed and we could actually chat? Well then, let’s do it!

If you haven’t already please check out Part 1 and Part 2 of this tutorial series.

So what do we need to add?

Well, in order to allow multiple connections to our server we need to add something called threads. Threads essentially are the things that allow multitasking on our computers and let us run more than one thing at a time. For example, you can listen to music while writing your essay, there is a thread running the music player and a thread running your word processor and then can perform their own functions separately. So, we can set up a thread for each person who connects to our server which will allow us to have tons of people to connect to our server. If you haven’t the slightest clue what a thread is then unfortunately this tutorial might not work out so well for you, try researching threads online (there are tons of tutorials out there about them) and then come back here.

Threads

Here is some code that will show you how we will be using threads to allow multiple connections:

1
2
3
Socket socket = sSocket.accept();
Thread socketThread = new ThreadClass(socket);
socketThread.start();

We will run this code every time someone connects and this will essentially place their connection in it’s own thread. This thread will handle everything for that one client. Unfortunately we can’t just say “RUN THE SOCKET” and it will suddenly magically receive connections from the client, print them out, and send them to all the other clients. So, we essentially create our own custom thread. Here is some code that will outline how this will work:

//We need to use "implements Runnable to tell Java that this is a thread
 class ClientThread implements Runnable {
 //This run method is what is executed when the thread starts
 public void run()
 {
 //Set up the PrintWriter and BufferedReader here
 while(true) {
 //Get info sent from client
 String clientInput = input.nextLine();
 //Here would would have a for loop that would send the
 //client's message to every other client connected.
 }
 }
 }
Okay, now that you know a little about threads and what we’re going to do with them lets redo the server code.

The New Server Code

import java.net.*;
import java.util.*;
import java.io.*;

public class Server
{
    public static void main(String[] args)
    {
        new Server();
    }

    public Server()
    {
        //We need a try-catch because lots of errors can be thrown
        try {
            ServerSocket sSocket = new ServerSocket(5000);
            System.out.println("Server started at: " + new Date());


            //Loop that runs server functions
            while(true) {
                //Wait for a client to connect
                Socket socket = sSocket.accept();



                //Create a new custom thread to handle the connection
                ClientThread cT = new ClientThread(socket);

                //Start the thread!
                new Thread(cT).start();

            }
        } catch(IOException exception) {
            System.out.println("Error: " + exception);
        }
    }

    //Here we create the ClientThread inner class and have it implement Runnable
    //This means that it can be used as a thread
    class ClientThread implements Runnable
    {
        Socket threadSocket;

        //This constructor will be passed the socket
        public ClientThread(Socket socket)
        {
            //Here we set the socket to a local variable so we can use it later
            threadSocket = socket;
        }

        public void run()
        {
            //All this should look familiar
            try {
                //Create the streams
                PrintWriter output = new PrintWriter(threadSocket.getOutputStream(), true);
                BufferedReader input = new BufferedReader(new InputStreamReader(threadSocket.getInputStream()));

                //Tell the client that he/she has connected
                output.println("You have connected at: " + new Date());

                while (true) {
                    //This will wait until a line of text has been sent
                    String chatInput = input.readLine();
                    System.out.println(chatInput);
                }
            } catch(IOException exception) {
                System.out.println("Error: " + exception);
            }
        }
    }
}

The Explanation

So for this new code you can see that we only have to change the server’s code. You can use the client code from the last tutorial to connect to the server. The main thing now is that, each time a socket connection is accepted, that socket is passed off to a thread which runs the socket. Once that socket has been passed off to the thread the server then waits for a new socket connection.

Defining and Starting a Thread

An application that creates an instance of Thread must provide the code that will run in that thread. There are two ways to do this:

  • Provide a Runnable object. The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example:
    public class HelloRunnable implements Runnable {
    
        public void run() {
            System.out.println("Hello from a thread!");
        }
    
        public static void main(String args[]) {
            (new Thread(new HelloRunnable())).start();
        }
    
    }
    
  • Subclass Thread. The Thread class itself implements Runnable, though its run method does nothing. An application can subclassThread, providing its own implementation of run, as in the HelloThread example:
    public class HelloThread extends Thread {
    
        public void run() {
            System.out.println("Hello from a thread!");
        }
    
        public static void main(String args[]) {
            (new HelloThread()).start();
        }
    
    }
    

Notice that both examples invoke Thread.start in order to start the new thread.

Which of these idioms should you use? The first idiom, which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread. The second idiom is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread. This lesson focuses on the first approach, which separates the Runnable task from the Thread object that executes the task. Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.

The Thread class defines a number of methods useful for thread management. These include static methods, which provide information about, or affect the status of, the thread invoking the method. The other methods are invoked from other threads involved in managing the thread and Thread object. We’ll examine some of these methods in the following sections.

What Is a Socket?

Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.

On the client-side: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. To make a connection request, the client tries to rendezvous with the server on the server’s machine and port. The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. This is usually assigned by the system.

A client's connection requestIf everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.

The connection is madeOn the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.

The client and server can now communicate by writing to or reading from their sockets.


Definition:A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.


An endpoint is a combination of an IP address and a port number. Every TCP connection can be uniquely identified by its two endpoints. That way you can have multiple connections between your host and the server.

The java.net package in the Java platform provides a class, Socket, that implements one side of a two-way connection between your Java program and another program on the network. The Socket class sits on top of a platform-dependent implementation, hiding the details of any particular system from your Java program. By using the java.net.Socket class instead of relying on native code, your Java programs can communicate over the network in a platform-independent fashion.

Additionally, java.net includes the ServerSocket class, which implements a socket that servers can use to listen for and accept connections to clients. This lesson shows you how to use the Socket and ServerSocket classes.

If you are trying to connect to the Web, the URL class and related classes (URLConnection, URLEncoder) are probably more appropriate than the socket classes. In fact, URLs are a relatively high-level connection to the Web and use sockets as part of the underlying implementation. See Working with URLs for information about connecting to the Web via URLs.

Network Service Discovery / Bonjour – Client

Last post we set up a Android Network Service Discovery server that will receive text messages from our client. Now we are ready to create the client that we will use to create the text messages and send to the server.

When creating the project choose Sdk version 16, Jelly Bean 4.1.

or

Edit the Manifest first to avoid errors when creating the MainActivity class.

Replace minimum with android:minSdkVersion=”16″

and add

1
2
"true" android:name="android.permission.INTERNET"/>
    "android.permission.ACCESS_WIFI_STATE" />

This will all be done in one file for simplicity.

 import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import android.app.Activity;
import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
 private String SERVICE_NAME = "Client Device";
 private String SERVICE_TYPE = "_letstalk._tcp.";
 
 private InetAddress hostAddress;
 private int hostPort;
 private NsdManager mNsdManager;
 
 private int SocketServerPort = 6000;
 private static final String REQUEST_CONNECT_CLIENT = "request-connect-client";
 
 private static final String TAG = "NSDClient";
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 
 mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
 mNsdManager.discoverServices(SERVICE_TYPE,
 NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
 }
 
 NsdManager.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);
 Log.d(TAG, "Host = "+ service.getServiceName());
 Log.d(TAG, "port = " + String.valueOf(service.getPort()));
 
 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(SERVICE_NAME)) {
 // 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: " + SERVICE_NAME);
 } else {
 Log.d(TAG, "Diff Machine : " + service.getServiceName());
 // connect to the service and obtain serviceInfo
 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);
 }
 };
 
 NsdManager.ResolveListener mResolveListener = new NsdManager.ResolveListener() {
 
 @Override
 public void onServiceResolved(NsdServiceInfo serviceInfo) {
 Log.d(TAG, "Resolve Succeeded. " + serviceInfo);
 
 if (serviceInfo.getServiceName().equals(SERVICE_NAME)) {
 Log.d(TAG, "Same IP.");
 return;
 }
 
 // Obtain port and IP
 hostPort = serviceInfo.getPort();
 hostAddress = serviceInfo.getHost();
 
 /* Once the client device resolves the service and obtains
 * server's ip address, connect to the server and send data
 */
 
 connectToHost();
 }
 
 
 @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);
 Log.e(TAG, "serivce = " + serviceInfo);
 }
 };
 
 private void connectToHost() {
 
 if (hostAddress == null) {
 Log.e(TAG, "Host Address is null");
 return;
 }
 
 String ipAddress = getLocalIpAddress();
 JSONObject jsonData = new JSONObject();
 
 try {
 jsonData.put("request", REQUEST_CONNECT_CLIENT);
 jsonData.put("ipAddress", ipAddress);
 } catch (JSONException e) {
 e.printStackTrace();
 Log.e(TAG, "can't put request");
 return;
 }
 
 new SocketServerTask().execute(jsonData);
 }
 
 private String getLocalIpAddress() {
 WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
 String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
 return ip;
 }
 
 private class SocketServerTask extends AsyncTask<JSONObject, Void, Void> {
 private JSONObject jsonData;
 private boolean success;
 
 @Override
 protected Void doInBackground(JSONObject... params) {
 Socket socket = null;
 DataInputStream dataInputStream = null;
 DataOutputStream dataOutputStream = null;
 jsonData = params[0];
 
 try {
 // Create a new Socket instance and connect to host
 socket = new Socket(hostAddress, SocketServerPort);
 
 dataOutputStream = new DataOutputStream(
 socket.getOutputStream());
 dataInputStream = new DataInputStream(socket.getInputStream());
 
 // transfer JSONObject as String to the server
 dataOutputStream.writeUTF(jsonData.toString());
 Log.i(TAG, "waiting for response from host");
 
 // Thread will wait till server replies
 String response = dataInputStream.readUTF();
 if (response != null && response.equals("Connection Accepted")) {
 success = true;
 } else {
 success = false; 
 }
 
 } catch (IOException e) {
 e.printStackTrace();
 success = false;
 } finally {
 
 // close socket
 if (socket != null) {
 try {
 Log.i(TAG, "closing the socket");
 socket.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 
 // close input stream
 if (dataInputStream != null) {
 try {
 dataInputStream.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 
 // close output stream
 if (dataOutputStream != null) {
 try {
 dataOutputStream.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }
 return null;
 }
 
 @Override
 protected void onPostExecute(Void result) {
 if (success) {
 Toast.makeText(MainActivity.this, "Connection Done", Toast.LENGTH_SHORT).show();
 } else {
 Toast.makeText(MainActivity.this, "Unable to connect", Toast.LENGTH_SHORT).show();
 }
 }
 }
 
 protected void onPuase() {
 if (mNsdManager != null) {
 mNsdManager.stopServiceDiscovery(mDiscoveryListener);
 }
 super.onPause();
 }
 
 @Override
 protected void onResume() {
 super.onResume();
 if (mNsdManager != null) {
 mNsdManager.discoverServices(
 SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
 }
 
 }
 
 @Override
 protected void onDestroy() {
 if (mNsdManager != null) {
 mNsdManager.stopServiceDiscovery(mDiscoveryListener);
 }
 super.onDestroy();
 }
 
}

SERVICE_NAME is a constant that we use to set the device name, the Google example doesn’t use these it picks the name for you which is the service name if more than one device has the same service name, which it will, the 2nd device has a (1) added to it and the 3rd would have (2) added to it and so on. Not sure if this is what made the Google version so flaky so I don’t use it, I give the server a name and the client a name, and use that.

SERVICE_TYPE is a custom name you make up (that must conform to a standard). This is the name of your app on the network, it doesn’t have to be the same name as your app, just every device that you want to see each other must use the same one. The service type specifies which protocol and transport layer the application uses. The syntax is “_[protocol]._[transportlayer].” You can name the protocol anything you want but leave the transportlayer the way it is.

Note: If you plan on publishing an app to the app store that uses NSD you should register your protocol to the International Assigned Numbers Authority (IANA). They manage a centralized, authoritative list of service types used by service discovery protocols such as NSD and Bonjour. If you intend to use a new service type, you should reserve it by filling out the IANA Ports and Service registration form.

REQUEST_CONNECT_CLIENT is a constant we use to tell the server what we want to do. At first this will be the only option, but later we will add a display message option.

Now with our tools laid out let’s walk through the logic.

We display our screen with setContentView(R.layout.main);

We create an instance of NsdManager called mNsdManager to use to discover and connect to our server.

We then use NsdManager to discover our service (and server) on the network using our SERVICE_TYPE, we define what type of protocol we are using (for NSD you use NsdManager.PROTOCOL_DNS_SD) and where to go after the service is successfully registered (a interface callback we create called mDiscoveryListener).

Next we define, initialize and implement the DiscoveryListener interface call back. We use this to get all the devices that have registered the service. Since it is an interface we have methods() we must implement (@Override) to receive the NsdServiceInfo for all the servers we discovered.

The methods are:

public void onDiscoveryStarted(String regType) {}
public void onServiceFound(NsdServiceInfo service) {}
public void onServiceLost(NsdServiceInfo service) {}
public void onDiscoveryStopped(String serviceType) {}
public void onStartDiscoveryFailed(String serviceType, int errorCode) {}
public void onStopDiscoveryFailed(String serviceType, int errorCode) {}

The second one is the only one we will really use, the rest are used for logging purposes.

@Override
public void onServiceFound(NsdServiceInfo service) {
    // A service was found! Do something with it.
    Log.d(TAG, "Service discovery success : " + service);
    Log.d(TAG, "Host = "+ service.getServiceName());
    Log.d(TAG, "port = " + String.valueOf(service.getPort()));

    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(SERVICE_NAME)) {
        // 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: " + SERVICE_NAME);
    } else {
        Log.d(TAG, "Diff Machine : " + service.getServiceName());
        // connect to the service and obtain serviceInfo
        mNsdManager.resolveService(service, mResolveListener);
    }
}

When any service is found (even if it is not ours we show all the NsdServiceInfo that we discovered and then we point out the service name (which is “Server Device” for what we are looking for) and port number for the service, even if it is not ours (if you have printers on the network or webcams you will see those here too).

Now we start parsing the NsdServiceInfo to find what we want. First we check the SERVICE_TYPE if it is NOT (notice the ! at the beginning of the if statement) _letstalk._tcp. then we log it and keep discovering.

If the SERVICE_TYPE IS _letstalk._tcp. we check the SERVICE_NAME. If the SERVICE_NAME is the same as ours, Client Device, we know we have discovered ourself, so we keep looking.

If the SERVICE_NAME is different than we have found another device that we can connect to. In our case it should be the server. This is where you would create a List if you wanted to make a list of several devices you could connect to, but for simplicity sake if we find our server we immediately resolve the service so we can connect.

To resolve the service we use our instance of NsdManager to start the resolveService() method which takes two parameters the first is the NsdServiceInfo of the device we are wanting to resolve(get IPAddress for), and the second parameter is where to send that information when we get it, which is the ResolveListener interface call back.

Now we define, initialize and implement the ResolveListener interface call back. It has two methods we must implement.

public void onServiceResolved(NsdServiceInfo serviceInfo) {}
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {}

onServiceResolved(NsdServiceInfo serviceInfo) is the one we care about, it gives us the NsdServiceInfo that includes the server IP address this time.

First we double check to make sure we didn’t get our own device’s address by checking the SERVICE_NAME, if we did we return, which goes back to resolving the next device we found.

If we did get the information for our server, we set our local variables that we will use to connect to the server, hostPort is the server port number and hostAddress is the servers IP address.

1
2
3
// Obtain port and IP
hostPort = serviceInfo.getPort();
hostAddress = serviceInfo.getHost();

We then run a custom method(), connectToHost();


private void connectToHost() {

    if (hostAddress == null) {
        Log.e(TAG, "Host Address is null");
        return;
    }

    String ipAddress = getLocalIpAddress();
    JSONObject jsonData = new JSONObject();

    try {
        jsonData.put("request", REQUEST_CONNECT_CLIENT);
        jsonData.put("ipAddress", ipAddress);
    } catch (JSONException e) {
        e.printStackTrace();
        Log.e(TAG, "can't put request");
        return;
    }

    new SocketServerTask().execute(jsonData);
}

We double check to make sure we have a IP address to connect to, if we don’t we jump out of the connectToHost() method so we don’t try to connect and crash.

If we do have a IP Address we run another custom method(), getLocalIpAddress(); Let’s jump there and then come back.

1
2
3
4
5
private String getLocalIpAddress() {
    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
    return ip;
}

First of all when you get any kind of Wifi information you are going to need request permissions from the user which means we will need a permission in the Manifest file.

1
"android.permission.ACCESS_WIFI_STATE" />

Then in the getLocalIpAddress() method we initialize our WifiManager which we will use to get our local IP address, wm.getConnectionInfo().getIpAddress().

Then we take that IP address that is not in a format we can use and run it through the Formatter class which contains a method() that turns the IP Address we retrieved into a String that we can use, Formatter.formatIpAddress().

And we send it back to the connectToHost() method and save it as ipAddress.

Now we are back in the connectToHost() method, we create a JSONObject that we can use to send data to the server.

There are lots of things we could do wrong, so we surround our actions with a try and catch and then start to put data in our JSONObject (jsonData). We use (key, value) pairs to do this so the command ends up looking like this:

1
2
jsonData.put("request", REQUEST_CONNECT_CLIENT);
jsonData.put("ipAddress", ipAddress);

The request “key” contains our REQUEST_CONNECT_CLIENT “value” which is request-connect-client. This will tell the server that we want to connect.

We also send a ipAddress “key” that contains our ipAddress “value” which is the IP address of our device.

We catch our JSONObject errors and print out our stack trace (e.printStackTrace();) if there were any, and jump out of our try with return;

But for fun’s sake, let’s say it worked and then we create a new custom Thread called SocketServerTask() and run it with our jsonData (.execute(jsonData);).

1
new SocketServerTask().execute(jsonData);

makes this:


private class SocketServerTask extends AsyncTask<JSONObject, Void, Void> {
    private JSONObject jsonData;
    private boolean success;

    @Override
    protected Void doInBackground(JSONObject... params) {
        Socket socket = null;
        DataInputStream dataInputStream = null;
        DataOutputStream dataOutputStream = null;
        jsonData = params[0];

        try {
            // Create a new Socket instance and connect to host
            socket = new Socket(hostAddress, SocketServerPort);

            dataOutputStream = new DataOutputStream(
                    socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());

            // transfer JSONObject as String to the server
            dataOutputStream.writeUTF(jsonData.toString());
            Log.i(TAG, "waiting for response from host");

            // Thread will wait till server replies
            String response = dataInputStream.readUTF();
            if (response != null && response.equals("Connection Accepted")) {
                success = true;
            } else {
                success = false;
            }

        } catch (IOException e) {
            e.printStackTrace();
            success = false;
        } finally {

            // close socket
            if (socket != null) {
                try {
                    Log.i(TAG, "closing the socket");
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            // close input stream
            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            // close output stream
            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (success) {
            Toast.makeText(MainActivity.this, "Connection Done", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "Unable to connect", Toast.LENGTH_SHORT).show();
        }
    }
}

As you noticed probably quite quickly we are using a AsyncTask class to send the data. AsyncTask is not a bare bones Thread it is actually a “helper” class that uses a Thread to do it’s work

Some detailed, but very good documentation from googles AsyncTask web page.

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

So in summary, AsyncTask uses a Thread and Handler, so you don’t have to, and posts it’s results to the UI Thread. It is also best for short tasks, like sending data, not listening for connections, like the server does.

private class SocketServerTask extends AsyncTask<JSONObject, Void, Void> {

The three parameters are used like this:

JSONObject is the type of Object we are passing in.
The first Void is the type of value you want to pass back to calculate the progress bar.
The second Void is the type of value you want to send back to the UI Thread to display.

Since we are using the AsyncTask to only do work, because what we are doing is going to happen so fast, we won’t be returning any values for the progress bar, and we don’t need to update the UI Thread because we will know it’s complete when the song starts playing.

We use the boolean value (true or false) success in multiple methods() in the SocketServerTask class so we define the variable here so we can get to it. We set it in our doInBackground() method and we retrieve it in our onPostExecute() method.

When we run the .execute() method we are actually running:

protected Void doInBackground(JSONObject... params) {}

protected means that it can not be called from outside the SocketServerTask class.

Void means we will not be passing any values to the onPostExecute() method. And also means we will be returning null from the doInBackground() method.

JSONObject… The … means array, so the doInBackground() method can take several JSONObjects in an array format.

params is the JSONObject (jsonData) we passed in from the connectToHost() method in the .execute(jsonData); command.

Now we do the same think like we did in our Server Thread:

We initialize our Socket, used to communicate with the server, and set it’s value to null. You may wonder why we initialize it here instead of farther down when we give it an actual value. When we are done using the socket we are going to try to close it from the finally {} section and if we don’t initialize it here we won’t be able to “see” it.

We initialize our DataInputStream, that receives incoming data, and set it to null.

We initialize our DataOutputStream, used to send data to clients, and set it to null.

We create a JSONObject jsonData and set it to the value in index 0 (the first value), which was passed in the params array of JSONObjects.

protected Void doInBackground(JSONObject... params) {}

Now we are about to do some pretty complicated stuff, Android makes it pretty easy, but if everything is not set up just right, it could easily fail, so we use a try and catch.

We create a new socket to talk to the server using the server IP Address we acquired and a port that we manually agreed upon.

Once we have the socket we can use it to create a DataInputStream and a DataOutputStream.

Now we can send data to the server.

dataOutputStream.writeUTF(jsonData.toString());

We take our dataOutputStream and write to the socket in a UTF-8 format which is a way to format text similar to the .xml files used for our layout objects, you will notice in the main.xml file at the beginning:

We convert our jsonData Object to a String, not exactly sure what it would look like, but it will contain two key/value pairs something like request request-connect-client ipAddress 192.168.1.98

then we run

String response = dataInputStream.readUTF();

Which blocks the Thread (which would cause our UI Thread to crash if we were running it there) and we wait until the server responds. Once we have a response, since we know it is only returning one string, not a JSONObject or any other object we can just compare the response to a text string.

if (response != null && response.equals(“Connection Accepted”)) {

We could have used a constant like:

final String CONNECTION_VALUE = “Connection Accepted” and then we could check if:

if (response != null && response.equals(CONNECTION_VALUE)) {

but that is a little more than we need at this point.

If the response was “Connection Accepted” then everything was successful and we can set the boolean value success to true, if the response didn’t equal “Connection Accepted” then we set the boolean value success to false.

Then we have our catch statement if something went wrong while performing I/O Input/Output and we can print our stackTrace if needed.

Finally which means after the Thread has performed it’s task we can try to close our socket, DataInputStream and our DataOutputStream. If we run into issues we catch the IOExceptions and print out our stackTrace for debugging.

After all that we have to return something because doInBackground() must have a return value, we are able to set it to Void like we did in this instance as a work around, but we still have to return something so we return null; which doesn’t go anywhere. Since we are returning Void the parameter we take in on the onPostExecute() method needs to be Void so it doesn’t expect a real value.

1
2
3
4
5
6
7
8
@Override
        protected void onPostExecute(Void result) {
            if (success) {
                Toast.makeText(MainActivity.this, "Connection Done", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "Unable to connect", Toast.LENGTH_SHORT).show();
            }
        }

Then we check our success variable and Toast a message to the screen accordingly.

For a better example of the onPostExecute() method let’s say that we are returning a Bitmap from the doInBackground() method.

This:

private class SocketServerTask extends AsyncTask<JSONObject, Void, Void> {

Would become this:

private class SocketServerTask extends AsyncTask<JSONObject, Void, Bitmap> {

This:

protected Void doInBackground(JSONObject... params) {

Would become this:

protected Bitmap doInBackground(JSONObject... params) {

This:

return null;

Would become this:

return bitmap; //bitmap being a Bitmap Object

This:

protected void onPostExecute(Void result) {

Would become this:

protected void onPostExecute(Bitmap result) {

To finish out this project we define the onPause(), onResume(), onDestroy() methods. These are all protected so you can only run them from this class. They all make sure mNsdManager has been created because if you try to stop a service when it doesn’t exist the program will crash.

onPause() will stop service discovery because if the app is not running there is no need to waste resources discovering things we won’t use (memory leak). Then the default onPause() method will run.

onResume() will continue discovering services, and reconnect to our server in this case, on resuming, after the default onResume() method runs.

onDestroy() will stop service discovery because if the app is not running there is no need to waste resources discovering things we won’t use (memory leak). Then the default onDestroy() method will run.

Here is the main.xml file

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

Now you can compile and run the server on one device and the client on the other, make sure both devices are on the same network. When you run the client you should see Toasts appear on both devices.

For my next post I will add the ability to send text from the client to the server to this project.

Network Service Discovery / Bonjour – Server

Before we get to the heart of this app, I will do a couple little apps that help you understand how to use NSD a little easier, Google’s example (nsdchat) is a full blown app with both the server and the client in the same one, along with fragments, classes buried in classes so the server can use the same logic as the client, it uses a Runnable Thread class which doesn’t handle reconnections (when orientations changes for instance) and for the most part it doesn’t work (flaky at best) and no documentation. You would think you would click on advertise on the server and detect and connect on the client, but that usually doesn’t work, you have to make both clients servers and then you can usually chat back and forth.

This little side app will strip out all that extra stuff and give you 2 apps (one client app and one server app).

You will start the server app and it will instantly start advertising itself.

You will start the client app and it will instantly detect the server and resolve it’s connection info and connect to the server.

Then you will be able to pass messages to the server which will display them.

Then we will dive into our first Apple code, Requires a Mac computer of some sort, which allows you to run Xcode, and you also need a Apple Developer License which you can get for free or they are $99 a year. The Apple code will allow us to act as a server and receive messages from the Android client.

This is what the MP3 player app will do also when we return to it, because all it does is send the song db number to the server which receives the number and plays the song it belongs to.

First, to use NSD you need to use a minimum of OS version of 16, JellyBean 4.1. This tutorial was tested on a JellyBean ASUS T700, KitKat Samsung S3, and a KitKat Samsung Note 3. The only issue seems to be the Note 3’s WIFI radio seems to drop connection more than it should, luckily NSD reconnects for you.

When creating the project choose Sdk version 16, Jelly Bean 4.1.

or

Edit the Manifest first to avoid errors when creating the MainActivity class.

Replace minimum with android:minSdkVersion=”16″

and add

<uses-permission android:required="true" android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

android.permission.INTERNET – let’s the user know that our application uses network sockets.

android.permission.ACCESS_WIFI_STATE – let’s the user know we are going to extract the WIFI IP Address.

Android NSD Server

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import android.app.Activity;
import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdManager.RegistrationListener;
import android.net.nsd.NsdServiceInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.*;
 
public class MainActivity extends Activity {
 
 private String SERVICE_NAME = "Server Device";
 private String SERVICE_TYPE = "_letstalk._tcp.";
 private static final String REQUEST_CONNECT_CLIENT = "request-connect-client";
 private SocketServerThread socketServerThread;
 private NsdManager mNsdManager;
 
 private int SocketServerPort = 6000;
 
 private List<String> clientIPs;
 
 private static final String TAG = "NSDServer";
 
 public void showToast(final String toast){
 MainActivity.this.runOnUiThread(new Runnable(){
 public void run(){
 Toast.makeText(MainActivity.this,toast,Toast.LENGTH_LONG).show();
 }
 });
 }
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 
 mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
 registerService(9000);
 
 clientIPs = new ArrayList<String>();
 socketServerThread = new SocketServerThread();
 socketServerThread.start();
 }
 
 public void registerService(int port) {
 NsdServiceInfo serviceInfo = new NsdServiceInfo();
 serviceInfo.setServiceName(SERVICE_NAME);
 serviceInfo.setServiceType(SERVICE_TYPE);
 serviceInfo.setPort(port);
 
 mNsdManager.registerService(serviceInfo,NsdManager.PROTOCOL_DNS_SD,mRegistrationListener);
 }
 
 RegistrationListener mRegistrationListener = new NsdManager.RegistrationListener() {
 
 @Override
 public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
 String mServiceName = NsdServiceInfo.getServiceName();
 SERVICE_NAME = mServiceName;
 Log.d(TAG, "Registered name : " + mServiceName);
 }
 
 @Override
 public void onRegistrationFailed(NsdServiceInfo serviceInfo,
 int errorCode) {
 // Registration failed! Put debugging code here to determine
 // why.
 }
 
 @Override
 public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
 // Service has been unregistered. This only happens when you
 // call
 // NsdManager.unregisterService() and pass in this listener.
 Log.d(TAG,
 "Service Unregistered : " + serviceInfo.getServiceName());
 }
 
 @Override
 public void onUnregistrationFailed(NsdServiceInfo serviceInfo,
 int errorCode) {
 // Unregistration failed. Put debugging code here to determine
 // why.
 }
 };
 
 private class SocketServerThread extends Thread {
 
 @Override
 public void run() {
 
 Socket socket = null;
 ServerSocket serverSocket = null;
 DataInputStream dataInputStream = null;
 DataOutputStream dataOutputStream = null;
 
 try { 
 Log.i(TAG, "Creating server socket"); 
 serverSocket = new ServerSocket(SocketServerPort);
 
 while (true) {
 socket = serverSocket.accept();
 dataInputStream = new DataInputStream(
 socket.getInputStream());
 dataOutputStream = new DataOutputStream(
 socket.getOutputStream());
 
 String messageFromClient, messageToClient, request;
 
 //If no message sent from client, this code will block the Thread
 messageFromClient = dataInputStream.readUTF();
 
 final JSONObject jsondata;
 
 try {
 jsondata = new JSONObject(messageFromClient);
 request = jsondata.getString("request");
 
 if (request.equals(REQUEST_CONNECT_CLIENT)) {
 String clientIPAddress = jsondata.getString("ipAddress");
 
 // Add client IP to a list
 clientIPs.add(clientIPAddress);
 showToast("Accepted");
 
 messageToClient = "Connection Accepted";
 
 
// Important command makes client able to send message
 dataOutputStream.writeUTF(messageToClient);
// ****** Paste here Bonus 1
 
// ****** Paste here Bonus 1
 } else {
 // There might be other queries, but as of now nothing.
 dataOutputStream.flush();
 }
 
 } catch (JSONException e) {
 e.printStackTrace();
 Log.e(TAG, "Unable to get request");
 dataOutputStream.flush();
 }
 }
 
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if (socket != null) {
 try {
 socket.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 } 
 
 if (dataInputStream != null) {
 try {
 dataInputStream.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 
 if (dataOutputStream != null) {
 try {
 dataOutputStream.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }
 
 } 
 
 }
 
 protected void onPuase() {
 if (mNsdManager != null) {
 mNsdManager.unregisterService(mRegistrationListener);
 }
 super.onPause();
 }
 
 @Override
 protected void onResume() {
 super.onResume();
 if (mNsdManager != null) {
 registerService(9000);
 }
 
 }
 
 @Override
 protected void onDestroy() {
 if (mNsdManager != null) {
 mNsdManager.unregisterService(mRegistrationListener);
 }
 super.onDestroy();
 }
 
}

SERVICE_NAME is a constant that we use to set the device name. If more than one device has the same service name, the 2nd device has a (1) added to it and the 3rd would have (2) added to it and so on. Not sure if this is what made the nsdChat version so flaky so I don’t use it, I give the server a name and the client a name, and use that.

SERVICE_TYPE is a custom name you make up (that must conform to a standard). This is the name of your app on the network, it doesn’t have to be the same name as your app, just every device that you want to see each other must use the same one. The service type specifies which protocol and transport layer the application uses. The syntax is “_[protocol]._[transportlayer].” You can name the protocol anything you want but leave the transportlayer the way it is.

Note: If you plan on publishing an app to the app store that uses NSD you should register your protocol to the International Assigned Numbers Authority (IANA). They manage a centralized, authoritative list of service types used by service discovery protocols such as NSD and Bonjour. If you intend to use a new service type, you should reserve it by filling out the IANA Ports and Service registration form.

REQUEST_CONNECT_CLIENT is a static constant we use to detect what the client wants to do. At first this will be the only option, but later we will add a display message option. Static means that you can access the constant value from other classes without having to instantiate the class. A class is like blue prints on how to build a house, if you use the blue prints to instantiate the house, you build the house. A static value is like a equation on how to figure square footage that happens to also be written on the blue prints. Just because the equation is written on the blue prints it doesn’t mean you have to build the house to use them.

Now we layout our tools we want to use.

SocketServerThread is a custom class that we use to add functionality to the Thread class. The functionality that we add listen’s for incoming connections and receive’s data from the client.

Threads allow us to perform work simultaneously along with the main Thread. The main Thread is what the app display runs on and what Android watches to detect misbehaving apps that it needs to shut down to keep the phone performing in a user friendly way. The server will be waiting for a connection to be requested and established and it can not do this on the main Thread or Android will shut it down.

NsdManager is what we use to make the server discoverable by other devices. The client devices use NsdManager to discover us and then resolve (connect) to us.

List clientIPs is a list of Strings that will hold the IP Addresses of all the connected clients. This is not required to make the app work, and will not be used by me, but might be helpful to you if you need to know that information.

A TAG file is used for logging, it helps catch your eye when looking at logcat while your app is running to make sure it is making it through all steps of your program. This was one of the few concepts that I had to use logging for, especially with the nsdchat app, but that was before I found the showToast method on stackOverflow, which I show you next.

public void showToast(final String toast) {
    MainActivity.this.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this, "Accepted", Toast.LENGTH_LONG).show();
        }
    });
}

This is one of the best tools ever! Especially if you are debugging on your phone. This allows you to Toast whatever you want to the screen from anywhere in the app. If you remember earlier when I was discussing Context I said you had to have access to the main screen to be able to see a Toast, and you can’t always get Context. But with this tool you don’t need to. You can pass any string you like to this method() but for this app I just Toast a static message of Accepted.

Now with our tools laid out let’s walk through the logic.

We display our screen with setContentView(R.layout.main); – Displayed at the bottom.

We create an instance of NsdManager called mNsdManager to use to make our server discoverable.

We run a custom method registerService().

This method initializes an instance of NsdServiceInfo that we use to assign our service name, service type, and service port.

We then use NsdManager to register our service (server) on the network using our NSDServiceInfo (which will be passed to the client during discovery and resolve, we define what type of protocol we are using (for NSD you use NsdManager.PROTOCOL_DNS_SD) and where to go after the service is successfully registered (a interface we create for callbacks called mRegistrationListener).

Next we define, initialize and implement the RegistrationListener interface. We use this to tell if we successfully registered the service. Since it is an interface we have methods() we must implement (@Override) to receive service status change call backs.

public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {

Is executed when a the service is successfully registered. We use the NsdServiceInfo we receive to Log the service name that was registered.(This would be helpful if you were registering multiple different services).

public void onRegistrationFailed(NsdServiceInfo serviceInfo,
int errorCode) {

Is executed when the registration fails, you could log the error if you need to, but we don’t in this example.

public void onServiceUnregistered(NsdServiceInfo serviceInfo) {

This is run if the client is turned off.

public void onUnregistrationFailed(NsdServiceInfo serviceInfo,
int errorCode) {

This is run if registration fails.

Next we define our custom background Thread that we use to accept connections, receive and send data.

We initialize our Socket, used to communicate with the clients, and set it’s value to null.

We initialize our ServerSocket, used to accept connections, and set it to null.

We initialize our DataInputStream, that receives incoming data, and set it to null.

We initialize our DataOutputStream, used to send data to clients, and set it to null.

We use a try and catch because a port may not be available or configured wrong.

We then create our ServerSocket that we use to listen for incoming connections. Which is a pre-defined integer variable (SocketServerPort = 6000).

while(true){ – is known as an infinite loop, while remains true as long as the app is running.

Now comes the main reason why we use a Thread.

socket = serverSocket.accept();

This command sits and waits till a connection comes in, if this were on the main thread it would cause the program to time out and crash. It’s called a blocking method.

When a connection does come in you can not use the same socket to talk with the client, because the ServerSocket is always waiting and listening for just incoming calls, so when a new connection is made a new socket is created and assigned to the socket variable

We can then use this socket to get a InputStream and OutputStream for the socket.

Then we wait for the client to send some data.

messageFromClient = dataInputStream.readUTF();

This is another blocking method that doesn’t allow the code to continue until data is received.

The data that the client sends us in this app is in a JSONObject format which is similar to a HashMap format, or a string value pair. It is essentially a variable name with a value. You can check for the existence of the variable, and if it exists get the value. The variable is the name for the value.

Since the client will be sending a JSONObject we create one to store the data passed in.

Since the data passed in might not end up being a JSONObject we surround it with a try and catch to debug issues if there are any.

We initialize the JSONObject by putting the data passed in from the client in it.

We then get the value (which is a String) out of the variable request.

If the value stored in request equals the constant stored in REQUEST_CONNECT_CLIENT, which is “request-connect-client”

Then we get the value stored in ipAddress (which is a String).

Then we take that value and add it to our List of client IP Adresses (which we do nothing with).

We then display a toast so we can tell the server received our message from the client.(to help with debugging)

We then create a message to send back to the client “Connection Accepted”.

We then send the message using writeUTF to send data out the OutputStream using the socket.

dataOutputStream.writeUTF(messageToClient);

if (request.equals(REQUEST_CONNECT_CLIENT)) is FALSE.

dataOutputStream.flush();

Which removes all data from the OutputStream, this will be useful for our MP3 player because we will be listening for a certain amount of data (20 characters) and if there are data remnants left in the pipe this could really mess up our data flow.

First we catch our last try statement which tries to pull data out of the JSONObject so we catch a JSONException. If there is an JSONException we do a printStackTrace() and flush any data that might have made it in the pipe.

Next we catch a IOException in case we have issues creating the Socket, InputStream, or OutputStream.

After making it through the Thread and only after making it through the Thread do we try to close the socket, close the OutputStream, and close the InputStream. (if they have been used( i.e. !=null))

Finally if another application is opened over this app.

protected void onPuase() {

If NsdManager is being used, we can stop advertising it’s existence.

mNsdManager.unregisterService(mRegistrationListener);

If the app is re-opened.

protected void onResume() {

and NsdManager is still available we can re-advertise our service.

registerService(9000);

Or if our app is completely removed from memory by the system, we can stop advertising it’s existence.

mNsdManager.unregisterService(mRegistrationListener);

That’s everything you need all in one file.

Except for the layout/main.xml file.

"http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:gravity="center"
 android:orientation="vertical" >

For this example we are connecting a Android client to a Android server using Network Service Discovery. We have set up the server. In my next post we will set up the client.

Android thread

Android modifies the user interface and handles input events from one single user interface thread. This thread is also called the main thread.

To provide a good user experience all potentially slow running operations in an Android application should run asynchronously

The Handler class can be used to register to a thread and provides a simple channel to send data to this thread.

To use a handler you have to subclass it and override the handleMessage() method to process messages.

Your thread can post messages via the sendMessage(Message) method or via thesendEmptyMessage() method to the Handler object.

Every app has its own special thread that runs UI objects such as View objects; this thread is called the UI thread. Only objects running on the UI thread have access to other objects on that thread. Because tasks that you run on a thread from a thread pool aren’trunning on your UI thread, they don’t have access to UI objects. To move data from a background thread to the UI thread, use a Handler that’s running on the UI thread.