androstep4
This is a bit cleaner, since it allows you to have your code in separate files, but for now, I find it less flexible and easy to implement too. Thus I used asyncTask, as follows:

private class sendOnNetwork extends AsyncTask {

    @Override
    protected Void doInBackground(String... params) {
        try {

            DatagramSocket s = new DatagramSocket();
            int length = params[2].length();
            byte[] message = params[2].getBytes();
            InetAddress ip = InetAddress.getByName(params[0]);
            int port = Integer.getInteger(params[1], 1042);

            Log.e("network", "hello");

            DatagramPacket p = new DatagramPacket(message, length, ip, port);
            s.send(p);
            Log.e("network", String.valueOf(length));
        } catch (UnknownHostException e) {
            e.printStackTrace();
            Log.e("network", "UnknownHostException");
        } catch (SocketException e) {
            e.printStackTrace();
            Log.e("network", "SocketException");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("network", "IOException");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void post){
        Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_SHORT).show();
        textToSend.setText("");
    }
}


Note:

<String, Void, Void> are the inputs/outputs of your task, as follows: are the inputs/outputs of your task, as follows: …AsyncTask… with

  • Params, the type of the parameters sent to the task upon execution.
  • Progress, the type of the progress units published during the background computation.
  • Result, the type of the result of the background computation.
Next Back

Android Item List – Step 4
Tagged on:         

2 thoughts on “Android Item List – Step 4

Leave a Reply