androstep5
Note:

text and socket are at the beginning of the function so we can use it in doInBackground and onPostExecute. The Build.VERSION.SDK_INT is here because, in older versions of Android, you could only launch one normal activity at the time, to avoid this problem, we launch the activity as Executor, which would be a kind on admin, as I understand it.

private class listenNetwork extends AsyncTask {

    String text;
    byte[] buff = new byte[4096];
    DatagramSocket socket;
    DatagramPacket datagramPacket = new DatagramPacket(buff, buff.length);

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

        Log.v("listenNetwork", "in");

        try {

            socket = new DatagramSocket(port);
            Log.v("listenNetwork", "trying");

            socket.receive(datagramPacket);
            text = new String(buff, 0, datagramPacket.getLength());
            Log.v("listenNetwork", text);

        } catch (SocketException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } ;
        return null;
    }

    @Override
    protected void onPostExecute(Void post){

        InetAddress sourceIp = datagramPacket.getAddress();

        addLog(sourceIp, new Date(), text);
        populateList();
        socket.close();

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR1)
            new listenNetwork().execute();
        else
            new listenNetwork().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}

Back Next

Android Item List – Step 5
Tagged on:         

2 thoughts on “Android Item List – Step 5

Leave a Reply